Incomplete inheritance of javascript tutorials

Source: Internet
Author: User

Incomplete inheritance of javascript tutorials

The inheritance of Javascript is very different from the inheritance of standard oop. the inheritance of Javascript uses the prototype chain technology. The following uses an example to learn about the inheritance of JS.

Javascript inheritance is very different from standard oop inheritance. Javascript inheritance uses prototype chain technology, each class puts "member variables" and "member functions" on prototype, and Js ++ links them through superclass, that is, C. prototype. superclass = C. superclass = P. prototype;

When var c = new C (), c. _ proto _ = C. prototype;

When c accesses the "member variable", if _ proto _ cannot be obtained, it will go to C. prototype lookup. If no prototype exists, the prototype of the parent class will be searched. Because only _ proto _ is allocated when the object is created (each object is allocated independently ), others are allocated when the definition is made (each object is shared). At this time, if you access C. in prototype, when the "member variable" is an object, the "member variable" itself is not modified, but the member of the "member variable" object is modified, members of the modified "member variables" object will be shared by all object instances, which violates the original intention of class design.

For example:

 

The Code is as follows:

'Package'. j (function (){

'Class A'. j (function (){

Jpublic ({

V: {a: 1}

});

Jprivate ({

P: {a: 1}

});

Jprotected ({

X: {a: 1}

});

 

});

 

'Class B extends A'. j (function (){

 

});

});

 

Var b1 = new B ();

B1.v. a = 5;

B1.x. a = 5;

Var b2 = new B ();

 

Console. log (b1.v. a) // The output is 5

Console. log (b1.x. a) // The output is 5

 

Console. log (b2.v. a) // The output is also 5, not as expected.

Console. log (b2.x. a) // The output is 1.

Console. log (b2.p. a) // unavailable, p does not exist

 

 

 

How can this problem be solved?

A. the member "member variable" such as "v" (which is an object itself) is not defined on the prototype chain, but called in the constructor. At this time, when an object instance is created, it will be allocated on _ proto _ of the object.

 

Js ++ provides similar methods. As long as the "member variables" or "member functions" defined in jprivate are allocated to the _ proto _ of the object, only this instance is available. The "member variable" defined in jprotected (which is an object itself) is also allocated to the _ proto _ of the object, and only inherits its availability,

 

B. The prototype chain only defines read-only "member variables" (the object itself)

 

C. The members in the "member variable" defined by jpublic (which is an object) are only read-only members. Do not assign values. Otherwise, they will be shared among instances.

 

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.