Home > Web Front-end > JS Tutorial > JavaScript prototype chain learning summary_javascript skills

JavaScript prototype chain learning summary_javascript skills

WBOY
Release: 2016-05-16 18:17:30
Original
868 people have browsed it
Function and Object are both instances of functions

Function’s parent prototype points to Function’s prototype, and the parent prototype of Function’s prototype is the prototype of Object.
The parent prototype of Object also points to the prototype of Function.
An instance object whose default parent prototype is the explicit prototype of its constructor
[Each object has an implicit property that points to its parent object (the constructed object function) prototype (here called the parent prototype or implicit prototype). Because prototypes are also objects, prototypes also have parent prototypes. The prototype of Object is the top level (prototype root) of all parent prototypes, thus forming the so-called prototype chain]

Object attribute access principles

When reading attributes from an object, if such an attribute does not exist in the object's own attribute list, it will look for it in its associated parent prototype object. If the parent prototype object attribute If there is no such attribute in the list, the parent prototype of this parent prototype will be searched until it is found or until the search for the top-level prototype [Object.prototype] object attribute list is completed
The method of calling the object is the same as the search process for accessing attributes. , because the function object of the method is an attribute value of the object.
Example:
Copy code The 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("Feathered Lion");
//@__proto_ _Attribute is a reference to the parent prototype
//@Object.prototype.__proto__=null
/*
n1’s prototype chain
n1.__proto__=Class1.prototype
Class1.prototype .__proto__=Object.prototype

*/
var n2 = new Class2();
/*
n2’s prototype chain
n2.__proto__=Class2.prototype
Class2.prototype.__proto__=Object.prototype
*/
n1.m1();//===Object.prototype.m1();
n2.m1();//=== Class2.prototype.m1();
alert(n1.p1);//Hairy Lion
alert(n2.p1);//undefined

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template