Câu hỏi phỏng vấn Javascript
Câu hỏi

What is the motivation for bringing Symbol to ES6?

Câu trả lời

The motivation for introducing the Symbol primitive type in ECMAScript 6 (ES6) was initially to enable private properties in JavaScript objects. However, this feature was downgraded, and Symbols did not end up providing true privacy since they can be discovered through reflection methods like Object.getOwnPropertySymbols or proxies[1][3]. Despite this, Symbols have become a valuable addition to the language for several reasons.

Symbols serve as a way to generate unique and non-colliding keys for object properties, which is particularly useful in avoiding name clashes. This is important because it allows developers to add new properties to objects without the risk of overwriting existing properties that might have the same name[1][4]. For instance, ECMAScript itself can introduce new methods or properties on objects using Symbols without conflicting with property names defined by users[1].

Symbols are also non-enumerable, meaning they do not show up in for-in loops or when using methods like Object.keys(), which can be beneficial when developers want to hide certain properties from enumeration[1][4]. Additionally, Symbols can be shared globally through a global Symbol registry using Symbol.for, allowing different parts of code to access the same Symbol instance by a given key[1].

Another aspect of Symbols is that they are immutable and cannot be instantiated like objects. They are a primitive data type, ...

middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What is the drawback of creating true private in JavaScript?

junior

Why is it, in general, a good idea to leave the global scope of a website as-is and never touch it?

senior

What is the new keyword in JavaScript?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào