First, the relationship between prototype and prototype object

In JavaScript, a function is also a special object. It also has properties and values. All functions have a special property called prototype whose value is an object. A prototype object has two attributes: constructor and __proto__

The prototype object of the function has the following characteristics:

1. By default, all function prototype objects have a constructor attribute that points to the constructor associated with them

2. The function’s prototype object also has its own prototype object, represented by a __proto__ attribute

3. Constructors are used to create objects, which are called instance objects. The instance object implements inheritance from the constructor’s prototype object by __proto__ pointing to it

4. Every object has a __proto__ attribute to indicate the prototype object it inherits, but only functions have the Prototype attribute

Access objects’ properties and methods through the stereotype chain

When JavaScript tries to access an object’s properties, it does a lookup based on the prototype chain, which looks like this:

1. The system searches for the object first. If it is not found, it searches for the prototype object of the object, and the prototype object of the prototype object of the object……

2. In JavaScript all objects come from Object, object.prototype. proto = null. Null has no prototype and serves as the last link in the prototype chain

3.JavaScript iterates through the entire prototype chain of the accessed object, and if it does not find the object, it assumes that the property value of the object is undefined

Scope chain

In JavaScript, scopes are divided into global scopes and local scopes

When a variable is evaluated, it is searched in the function scope that created the variable. If it cannot be found, it is searched in the upper scope until the global scope is found

4. The difference between prototype chain and scope chain

1. Scope chain is relative to variables, and prototype chain is relative to attributes of objects; 2. The top layer of the scope chain is Window, and the top layer of the prototype chain is Object