Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of the double brackets for the [[prototype]] property in JavaScript?

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?

like image 442
Geek Avatar asked Jun 18 '13 17:06

Geek


People also ask

What does it mean when there are double brackets?

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.

What do the brackets mean in JavaScript?

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.

What do double square brackets mean?

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.

What are square brackets used for in JavaScript?

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.

What is the difference between prototype and property in JavaScript?

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.

What do all JavaScript objects inherit from a prototype?

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

What is the prototype of rectangle in JavaScript?

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.

What happens if there is no match between an object and prototype?

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.


1 Answers

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).

like image 103
apsillers Avatar answered Sep 25 '22 06:09

apsillers