Both Function and Object are Function instances.
The parent prototype of a Function points to the prototype of a Function. The parent prototype of a Function is the prototype of an Object.
The Object parent prototype also points to the Function prototype.
An instance's portrait. Its default parent prototype is the display prototype of its constructor.
[Each object has a hidden object attribute that is used to point to its parent object (a function for constructing the object). This is called a parent object or an implicit object ). Because the prototype is also like a prototype, the prototype also has a parent prototype. The prototype of an Object is the top layer (prototype root) of all parent prototypes, thus forming the so-called prototype chain]
Image attribute access principles
When reading an attribute from an object, if the object does not exist in its own attribute list, it will search for the parent object associated with it, if the parent prototype does not have such a property in the property list, it searches for the parent prototype of the parent prototype until it is found or until the parent prototype [Object. prototype] The image attribute list has been searched.
The method of calling an object is the same as the process of accessing attribute search, because the function object of a method is an attribute value of an object.
Instance:
Copy codeThe Code is as follows:
Object. prototype. m1 = function (){
Alert ("I am a lion ");
}
Function Class1 (str ){
This. p1 = str;
}
Function Class2 (){}
Class2.prototype. m1 = function (){
Alert ("hello ");
}
Var n1 = new Class1 ("Mao Lions ");
// @__ Proto _ the property is a reference to a prototype like a parent.
// @ Object. prototype. _ proto __= null
/*
Prototype chain of n1
N1. _ proto __= Class1.prototype
Class1.prototype. _ proto __= Object. prototype
*/
Var n2 = new Class2 ();
/*
Prototype chain of n2
N2. _ proto __= Class2.prototype
Class2.prototype. _ proto __= Object. prototype
*/
N1.m1 (); // = Object. prototype. m1 ();
N2.m1 (); // === Class2.prototype. m1 ();
Alert (n1.p1); // Mao lions
Alert (n2.p1); // undefined