In fact, prototype can be summarized in just a few words:
Any prototype is an object, and only the object has the original type.
Only the Function has the prototype attribute. It is the prototype inherited by the object generated when the Function is used as the constructor. The prototype of a Function has nothing to do with its prototype attribute.
The Object prototype can be accessed through the non-standard attribute _ proto _ or ECMAScript5 Object. getPrototypeOf.
1 is actually wrong. The Object at the end of the prototype chain has no prototype. But for a simpler expression. After looking at the prototype chain, you will understand how such undefined methods as. toString () come from.
The ambiguity mentioned above is the understanding of the text, and the syntax itself is unambiguous. Prototype indicates a prototype, but the prototype of an object is not accessed by prototype.
Function has the prototype attribute, but it has nothing to do with its own prototype. After understanding this, you can read more articles about prototype chain and inheritance.
Here are some examples to help you better understand:
Copy codeThe Code is as follows:
// Any object has the original type
Obj = {};
Console. log (obj. _ proto __);
Console. log (Object. getPrototypeOf (obj ));
Console. log (obj. _ proto _ = Object. getPrototypeOf (obj ));
// Prototype attribute with no syntax significance for the object
Alert (obj. prototype) // undefined
// Prototype, as an attribute, only exists in the Function, which indicates the prototype integrated with the new instance created with this Function. It is irrelevant to the prototype of the Function.
Var F = function (name ){
This. name = name;
}
Obj = {a: 3,
Get B (){
Return this.;
}
};
F. prototype = obj;
NewObj = new F ('new name ');
NewObj. name; // used as the constructor. name is the attribute of newObj.
NewObj. a; // 3
// It inherits obj. It can be confirmed as follows:
Object. getPrototypeOf (newObj) === obj; // true
NewObj. _ proto _ = obj; // true