The inheritance of JavaScript

Source: Internet
Author: User

JavaScript's prototype chain;

JavaScript prototype chain, I would like to understand it, but has not understood, today seriously studied the next tutorial, the prototype chain of their own understanding of the language under the description;

There is a basic pattern for implementing a prototype chain, and its code is basically as follows:

functionsupertype () { This. property =true;} SuperType.prototype.getSuperValue=function(){    return  This. Superproperty;};functionsubtype () { This. Subproperty =false;}//inherited super.Subtype.prototype =Newsupertype (); SubType.prototype.getSuperValue=function(){    return  This. Subproperty;} varInstance =Newsubtype (); alert (Instance.getsupervalue)//true;

The search process is probably as follows: find it on the instance first, and then look it up in the prototype object. is always a ring buckle. Until the end of the prototype chain can be stopped;

1.1 Don't forget the default prototype;

The default prototype for all functions is an instance of object, so the default prototype will have a pointer to Object.prototype. This is why all custom types inherit the ToString () method;;

1.2 Determine the relationship between the prototype and the instance;

The relationship between the prototype and the instance can be judged in two ways; One is the instanceof operator, as long as this operator, to test the instance and the occurrence of the constructor;

There is also a way to isprototypeof ();

Alert (object.prototype.isPrototypeof (instance)); // truealert (Supertype.prototype.isPrototypeof (instance)); // true; Alert (Subtype.prototype.isPrototypeof (instance));//true;

1.3 a prudent definition method;

To override the method of a superclass or to add a method of a superclass, be sure to replace the superclass's instance with a subclass of prototype to add or modify the method, and a point where the modified method is called only when the subclass is called, and the superclass's

Instance calls or the original method; The reason I understand is that the superclass is just a prototype of the subclass, and the prototype of the superclass is still unchanged;

There is also a note that the prototype can not be defined by the object literal when it is implemented by the prototype chain, because it cuts off the connection between subclasses and superclass;

The prototype of the sub-class is directed directly to an instance of object;

1.4 Problem of the prototype chain;

The inheritance of JavaScript

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.