JS prototype chain

Source: Internet
Author: User
Please copy the following code directly to the browser ConsolePanel execution View Results
Console.log (");
Console.log ("); Console.log (' *********************************** constructor declaration ********************************************************
***');
Console.warn ('-------a total of 2 constructors, of which MyObjectEx1 and MyObjectEx2 respectively inherit MyObject-------');
Console.warn (' constructor 1:myobject ');  
function MyObject () {this.xxx = new Array ();  

} MyObject.prototype.yyy = new Array ();

Console.warn (' constructor 2:myobjectex1 ');  
function MyObjectEx1 () {this.zzz = new Array ();}  
Myobjectex1.prototype = new MyObject ();
MYOBJECTEX1.PROTOTYPE.AAA = new Array ();
MyObjectEx1.prototype.constructor = MyObjectEx1;
Console.log (");
Console.log ('-----------------------instantiation variable: MyObject------------------------------');
var obj1 = new MyObject ();
var obj2 = new MyObject ();
Obj1.xxx.push (' I am MyObject's public attribute zzz, I am obj1 ');
Obj1.yyy.push (' I am MyObject's archetypal attribute yyy, each instance of me inherits my attribute ');

Obj2.xxx.push (' I am MyObject's public attribute zzz, I am obj2 ');
Console.warn ("var obj1 = new MyObject ();");
Console.warn ("var obj2 = new MyObject ();"); Console.warn ("Obj1.xxx.push(' I am the public property of MyObject zzz, I am obj1 '); "
Console.warn (' I am the archetypal attribute yyy of MyObject, each instance of me inherits my attribute ');
Console.warn (' I am the public property of the MyObject zzz, I am obj2 ');
Console.warn ('-------------The result of the above instantiation variable is printed-------------');
Console.log (' obj1: ', obj1);

Console.log (' obj2: ', obj2);
Console.log (");
Console.warn ('-----------------------instantiation variable: MyObjectEx1-----------------------');
var objEx1 = new MyObjectEx1 ();
var objEx2 = new MyObjectEx1 ();
Console.warn ("var objEx1 = new MyObjectEx1 ();");
Console.warn ("var objEx2 = new MyObjectEx1 ();");
Console.warn ('-------------change the value of attributes in ObjEx1 and objEx2 '-------------');
ObjEx1.zzz.push (' I am MyObjectEx1 's public property, my value is objEx1 ');
ObjEx2.zzz.push (' I am MyObjectEx1 's public property, my value is objEx2 ');
ObjEx1.xxx.push (' Add an element to the parent class xxx via objEx1 ');
ObjEx2.xxx.push (' Add an element to the parent class xxx via objEx2 ');
ObjEx1.yyy.push (' Add an element to the parent class yyy through ObjEx1 ');
ObjEx2.yyy.push (' Add an element to the parent class yyy through ObjEx2 ');
Console.warn (' I am the public property of MyObjectEx1, my value is objEx1 '); Console.warn (' I am the public property of MyObjectEx1, my value is objEx2 '); ");
Console.warn ("Add an element to the parent class XXX through the objEx1" ObjEx1.xxx.push);
Console.warn ("Add an element to the parent class XXX through the objEx2" ObjEx2.xxx.push);
Console.warn (ObjEx1.yyy.push (' Add an element to the parent yyy by ObjEx1 ');
Console.warn (ObjEx1.yyy.push (' Add an element to the parent yyy by ObjEx2 ');
Console.warn ('-------------The result of the above instantiation variable is printed-------------');
Console.log (' objEx1: ', objEx1);

Console.log (' objEx2: ', objEx2); Console.warn (' It can be seen that for the public property in each constructor in the inherited relationship node, in the constructor of its subordinate (inheriting the constructor), the constructor public property is embodied as a ' Special ' attribute, although everyone has this attribute, but the value of this property is held by the instance, through '. ' In a way that directly values or assigns a value to it, but for different instantiation objects, it may be different, but for the stereotype attribute defined by prototype, it can be regarded as a ' local global variable ' for all subclasses, which can be viewed as a reference, which is distinct '.


1. A prototype chain is a chain of objects consisting of a finite object that implements inheritance and shared properties. 2. For instanceof this keyword, the more important meaning is to check the attribution of an object, that is, inheritance relationship, and create the variable of the class of the relationship between the province is not much, new process is actually more embodies the inheritance relationship (Maintenance prototype chain), We can maintain the Obj.constructor property for the instance object to point to the constructor of the class to achieve the most complete prototype chain effect, and secondly for a class, the constructor (Function) holds a prototype property, and when we new the instance object, It will also maintain the invisible attribute __proto__ attribute for its instance, and the prototype of the class to realize the mapping relation, for the __proto__ attribute, it embodies the characteristic of the tree, each inheritance level has its own corresponding attribute and method (the important core of using the prototype is the establishment of the inheritance tree. This process requires the participation of the new operator, which simply means that "subclass. Prototype" must be assigned to an instance of "new Parent ()" 3. For the typeof operator, it emphasizes the latter type, the so-called type, which is the data type of JavaScript, which may result in string, number, Function, Object (Null), Undefined, and one Boolean. 4. The inheritance hierarchy of each class instance in the so-called complete prototype inheritance chain should have its own constructor function, that is to say, the prototype chain is the most complete prototype chain for all classes in a prototype chain that have constructors. Second, when the keyword function is decorated with the prototype attribute, it can be seen that prototype is used to embody the extension method or inheritance relationship of the class. 5. Each variable or object has a __proto__ attribute, and this property is not visible in most browsers, only manual way to view, __proto__ attribute is an example in the prototype chain embodiment, any object has __proto__ attribute, to reflect the nature of the chain, The top level is object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.