I know that every JavaScript object has an internal property called [[Prototype]]
. Some implementations allow access to it via a property called __proto__
while other do not. Is there any special significance of the brackets surrounding this property?
Answer and Explanation: Double brackets or [[]] in math refer to rounding off the value inside to its greatest integer less than or equal to the value.
Bracket notation is more expressive than dot notation because it allows a variable to specify all or part of the property name. This is possible because the JavaScript interpreter automatically converts the expression within the square brackets to a string, and then retrieves the corresponding property.
Items in a list are separated by commas. A table is a list of lists, where the first list is the first row, the second list is the second row, and so on. Double Square Brackets [[ ]] Used to select items from a variable that contains a list, table, or matrix.
The [] operator converts the expression inside the square brackets to a string. For instance, if it is a numeric value, JavaScript converts it to a string and then uses that string as the property name, similar to the square bracket notation of objects to access their properties.
There is something misleading in JavaScript regarding prototypes. The prototype of an object is not the same thing as the prototype property of the object. The former is used when looking up non-existent properties in the prototype chain. The latter is used for objects created using new, it will be the prototype of the newly created object.
All JavaScript objects inherit properties and methods from a prototype: Date objects inherit from Date.prototype Array objects inherit from Array.prototype Person objects inherit from Person.prototype
In our example, Rectangle.prototype will be the the prototype value used for objects created with new Rectangle () and the prototype of Rectangle itself is actually JavaScript's internal Function.prototype. The value holding the prototype of an object is sometimes called the internal prototype link.
If after consulting both the object and its [ [Prototype]] still no match is found, JavaScript will check the prototype of the linked object, and continue searching until the end of the prototype chain is reached. At the end of the prototype chain is Object.prototype. All objects inherit the properties and methods of Object.
It is an "internal property" of the object. From ECMAScript 8.6.2:
This specification uses various internal properties to define the semantics of object values. These internal properties are not part of the ECMAScript language. They are defined by this specification purely for expository purposes. An implementation of ECMAScript must behave as if it produced and operated upon internal properties in the manner described here. The names of internal properties are enclosed in double square brackets [[ ]].
The statement, "These internal properties are not part of the ECMAScript language," means that internal properties are not identifiers that can be used in actual code -- internal properties are not accessible as members of the objects that contain them. However, they may be made accessible by particular functions or properties (e.g., some browsers are kind enough let you set and get [[Prototype]]
through the __proto__
property, and the ES5 spec allows read-only access through Object.getPrototypeOf
).
The use of double brackets over single brackets is probably to avoid any possible confusion with actual bracket notation (i.e., property access).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With