Prototype chaining Based on JavaScript

Source: Internet
Author: User

If you use a prototype to redefine the classes in the previous example, they will become in the following form:
Copy codeThe Code is as follows:
Function ClassA (){
}

ClassA. prototype. color = "blue ";
ClassA. prototype. sayColor = function (){
Alert (this. color );
};

Function ClassB (){
}

ClassB. prototype = new ClassA ();

The magic of prototype is the last line of code. Set the prototype attribute of ClassB to the instance of ClassA. This is interesting because you want to add all the attributes and methods of ClassA to the prototype attribute of ClassB one by one. Is there a better way to grant the prototype attribute to the ClassA instance?

Note:The ClassA constructor is called without passing parameters. This is a standard practice in the prototype chain. Make sure that the constructor does not have any parameters.

Similar to object impersonating, all attributes and methods of the subclass must appear after the prototype attribute is assigned a value, because all methods assigned before it are deleted. Why? Because the prototype attribute is replaced with a new object, the original object added with the new method will be destroyed. Therefore, the code for adding the name attribute and sayName () method to the ClassB class is as follows:
Copy codeThe Code is as follows:
Function ClassB (){
}

ClassB. prototype = new ClassA ();

ClassB. prototype. name = "";
ClassB. prototype. sayName = function (){
Alert (this. name );
};

You can test this code by running the following example:
Copy codeThe Code is as follows:
Var objA = new ClassA ();
Var objB = new ClassB ();
ObjA. color = "blue ";
ObjB. color = "red ";
ObjB. name = "John ";
ObjA. sayColor ();
ObjB. sayColor ();
ObjB. sayName ();

In addition, the instanceof operator is also unique in the prototype chain. For all instances of ClassB, if instanceof is ClassA or ClassB, true is returned. For example:
Copy codeThe Code is as follows:
Var objB = new ClassB ();
Alert (objB instanceof ClassA); // outputs "true"
Alert (objB instanceof ClassB); // outputs "true"

This is an extremely useful tool in the weak type world of ECMAScript, but this method cannot be used to determine when an object is impersonated. However, because the prototype of the subclass is directly re-assigned, the following situation occurs:
Copy codeThe Code is as follows:
Console. log (objB. _ proto __== objB. constructor. prototype)/false

Because the prototype attribute of the ClassB prototype chain is overwritten by the object of another class. The output shows that objB. _ proto _ still points to ClassB. prototype instead of objB. constructor. prototype. This is also very understandable, for Person. prototype assigns a value to a new ClassA () instance for an Object. The constructor of an Object defined by the Object quantity method points to the root constructor Object. prototype is an empty object. prototype.

Related Article

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.