📜 ⬆️ ⬇️

ECMA-262 standard (JavaScript) in pictures, part 3


In the previous parts, we examined the execution context structures, Function objects, and the this pointer . The third part deals with prototype inheritance.


How classes are implemented


There are no classes in JavaScript. Where in classical OOP classes and objects are used in JavaScript, objects with functions and objects with data are used.


')
When searching for properties , the [[Prototype]] internal field is used (it is available outside as __proto__ ). If the property is not found in the object, then it is searched in the prototype.
var a = { n: 1 }; var b = { __proto__: a, m: 2 }; console.log(bn); //  1 

So that every time you create an object you do not need to explicitly specify __proto__ in JavaScript, you can use functions as constructors using the new operator. When interpreting the function keyword, each time , not only the Function object is created, but also the Prototype object associated with it .



If you call a function as new User() , and not just User() , then a new object will be created. The __proto__ property of the new object will point to the prototype function.



When you call a function via the new ThisBinding, the context will point to the created object. If you write this.name = "Unknown" in the function User , then the name field with the specified value appears in the new object.
"Class methods" are logical to place in the prototype, in this case all "class objects" will have access to them.
 function User(name) { this.name = name; } User.prototype.getName = function _getName() { return this.name; } var user = new User("John"); console.log(user.getName()); 

Since the “point” call is used when calling “methods”: user.getName() , thisBinding will point to a new object and return this.name will return the value of the corresponding name value.



It is important to distinguish between _proto _ and prototype . The __proto__ ( [[Prototype]] ) property is on all objects and is used to search for properties. The prototype property is only for Function objects and is used when creating objects through new .


How class inheritance is implemented


JavaScript inheritance is implemented using a chain of prototypes.



Suppose you need to inherit from the class Useree and add another “method”. For this you need:

In this case, if the function is not found in one prototype, the search will be continued in the "parent prototype".



However, long chains of inheritance in JavaScript are not welcome.
More about prototype inheritance in the standard:

Conclusion


Read the standard, nothing complicated there. The main thing to understand the basic concepts and relationships between them. I recommend to start with something clear, for example, with a function call . Then - deal with the definitions in the text. Recursively repeat.

It also makes sense to look at the first releases of the standards . First, it is interesting to observe the evolution. And secondly, some features appeared later, so the standards themselves were simpler. For example, there is no separation between LexicalEnvironment and VariableEnvironment , which is somewhat confusing.

If at least one developer, after reading the article, understands the standard, I can consider my task accomplished.

Source: https://habr.com/ru/post/280137/


All Articles