Prototype and prototype chain
(1) Prototype
== Understanding and memorizing core == :
Constructor === f == F. __proto__ == The prototype object (prototype). Constructor === f == F. __proto__. Constructor === f == F. __proto__Copy the code
Explanation:
- Every function F (also an object, everything is an object in JS) has a prototype property when initialized, which points to the prototype object.
F = new Function(a); F = {prototype:{}
};
F.prototype = {} // Prototype object (prototype)
Copy the code
- Each constructor instance F (more broadly, every object, and instance objects are also objects) has an internal __proto__ attribute pointer (except null) to the prototype object (that is, the prototype). (__proto__ is also called the implicit prototype of the instance object.)
var f = new F();//f is the instantiation object of the constructor f
f = {
__proto__:{}
}
f.__proto__ = {} // Prototype object (prototype)
Copy the code
- Stereotypes: Each object inherits some common properties and methods from the stereotype
var f1 = new F('mike');
var f2 = new F('jack');
f1.__proto__ === f2.__proto__ ==={};// There are some public properties and methods in it
Copy the code
A small extension
1. There is no concept of inheritance in the original JS, and objects (F1 and F2) cannot share attributes and methods. 2. Every time we create a new object, we allocate a new chunk of memory to it, which is a huge waste of resources. 3. With this in mind, JavaScript designer Brendan Eich decided to set a property for the constructor. 4. This property refers to an object in which all properties and methods that need to be shared by instance objects are stored. Those that need not be shared are stored in constructors. 5. Once an instance object is created, its properties and methods are automatically referenced. That is, the properties and methods of instance objects are divided into two types, one is not shared, and the other is shared public objects. This object is the Prototype object, or prototype for short.
- Each prototype object has a constructor property pointer to the constructor F itself
var f = new F();
f.__proto__ = {
constructor:F
}
Copy the code
- Because the instance object does not have a constructor attribute, it is looked up on the stereotype
// The stereotype object has the constructor attribute, not the instance object
f.constructor === f.__proto__.constructor === F.prototype.constructor
f.__proto__.constructor === F;
// This is how instance object f actively finds its constructor, f, because of the link layer
Copy the code
Graph (from the Internet to find a map, easy to understand) referenceBlog.csdn.net/cuckooii/ar…
(2) Prototype chain
understand
- When == reads the properties of the instance object (or any object) ==
- If you can’t find it, you look it up on the prototype
- If you can’t find it again, you go to the prototype of the prototype object, all the way to the top level
- The layer upon layer chain relationship between the == instance and its prototype thus constituted is called the prototype chain ==
- The prototype chain has an end. It doesn’t go on forever.
/ / when
Object.prototype.__proto__ === nullWhen,// undefined is returned.
Copy the code
(3) Summary expression
What are prototypes and prototype chains? Prototype:
// You can either write it out or think about it yourself
F.prototype === f.__proto__;
f.__proto__.constructor === F;
Copy the code
- Each constructor created will have a prototype that points to the prototype object
- The constructor instantiates different instance objects through new
- Each object has a __proto__ attribute that points to its protoobject (which is the same object).
- The constructor property in the prototype object points to the constructor
- The Prototype object is a common container accessible to all instantiated objects of the same constructor
The prototype chain:
- When an object’s properties cannot be read, it goes back to its prototype, or even its prototype
- The layer upon layer chain relationship between the instance thus constituted and its prototype is called prototype chain
———————————— To be added later