JavaScript Object----Inheritance

Source: Internet
Author: User

JavaScript objects have "own properties", and some properties are inherited from prototype objects. To better understand this inheritance, you must have a deeper understanding of the details of property access.

Suppose you want to query the property X of the object o, and if X is not present in O, the property x will be queried in the prototype object of O. If there is no X in the prototype object, but the prototype object also has a prototype, then the query will continue to be executed on the prototype object's prototype until the X is found or the object location where the prototype is null is located. As you can see, the prototype property of an object forms a "chain" that enables inheritance of attributes through this "chain".

var o = {}//o method for inheriting objects from object.prototype o.x = 1;   Define an attribute X for O, with a value of 1var p = Inherit (o);       P Inherits O and object.prototypep.y = 2;  Define a property Y for P, with a value of 2var q = Inherit (o);        Q inheritance P, O and object.prototypeq.z = 3;   Define a property Z for Q, with a value of 3var s = q.tostring (); ToString inherits from object.prototypeq.x + q.y//result is 3,x and Y are inherited from O and P respectively

Now suppose that the attribute X of the object o is assigned a value, if there is already an attribute X in O (this attribute is not inherited), then this assignment only changes the value of the existing attribute x. If the attribute x does not exist in O, then the assign operation adds a new attribute x to O. If the previous o inherits from attribute X, then the inherited property is overwritten with the newly created property with the same name.

The property assignment operation first checks the prototype chain to determine whether the assignment operation is allowed. For example, if O inherits from a read-only attribute x, then the assignment operation is not allowed. If an attribute assignment operation is allowed, it always creates a property on the original object or assigns a value to an existing property, without modifying the prototype chain.

In JavaScript, inheritance exists only when querying properties, and setting properties is irrelevant to inheritance, which is an important feature of JavaScript, which allows programmers to selectively overwrite inherited properties.

var unitcircle = {r:1};   An object to inherit var c = Inherit (unitcircle); c Inheritance Attribute rc.x = 1;             C.Y = 1;                      C defines two properties C.R = 2;                 C Overwrite the inherited attribute UNITCIRCLE.R; The result is 1, the prototype object has not been modified

Property assignment either fails, either creates a property or sets a property in the original object, but with one exception, if O inherits from Property X, and this property is a accessor property with setter methods, then the setter method is called instead of creating an attribute X for O. It is important to note that the setter method is called by the object o, not the prototype object that defines the property. So if the setter method defines any attribute, this operation only targets the O itself and does not modify the prototype chain.

JavaScript Object----Inheritance

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.