JavaScript Tutorial Incomplete Inheritance (JS prototype chain) _ Basics

Source: Internet
Author: User
Tags inheritance

JavaScript inheritance and standard OOP inheritance are very different, JavaScript inheritance is the use of prototype chain technology, each class will be "member variable" and "member function" on the prototype, js++ have superclass link it up, that is C.prototype.superclass = C.superclass = P.prototype;
When var C = new C (), c.__proto__ = C.prototype;
When C accesses a member variable, if the __proto__ cannot get it, it goes to the C.prototype lookup, and if it does not, it goes to the prototype of the parent class, because only __proto__ is allocated when the object is created (each object is allocated independently). All others are allocated when defined (shared by each object), at this point, if you are accessing a member variable in C.prototype that is an object, not modifying the member variable itself, but modifying the members of the member variable object, the members of the modified member variable object are shared by all object instances. This violates the original intention of the class design.
For example:

Copy Code code 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)//output is 5
Console.log (B1.X.A)//output is 5

Console.log (B2.V.A)//output is also 5, not the expected 1
Console.log (B2.X.A)//output is 1
Console.log (B2.P.A)//unavailable, will prompt p does not exist

How do I solve this problem?
A. A member "member variable" such as V, which is itself an object, is not defined on the prototype chain, but is called in the constructor, where an object instance is created and allocated on the object's __proto__.

Js++ provides a similar method, as long as the "member variable" or "member function" defined in Jprivate is assigned to the __proto__ of the object, and only this instance is available, the "Member variable" (which itself is an object) defined in jprotected is also assigned to the object's __ On the proto__, and only to inherit his availability,

B. Only read-only "member variables" are defined on the prototype chain (which is itself an object)

C.jpublic-defined member variables, which are themselves objects, are members of only read-only members, remember that they are not assignable, otherwise they are shared in individual instances.

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.