Introduction to Reading and writing of object property in JavaScript _javascript tips

Source: Internet
Author: User
Tags inheritance

In JavaScript, you can use the dot operator "." or the bracket operator "[]" to read and write the property of an object:


Copy Code code as follows:

var o = {x:1, y:2};
Console.log (o.x);//1
Console.log (o["y"]);//2
O.Y = 7;
Console.log (o["y"]);//7


It is important to note that if you use the bracket operator, the value type within the operator must be string, or an expression that can be converted to string:


Copy Code code as follows:

Console.log (O[y]);//referenceerror:y is not defined
var v = "Y";
Console.log (O[v]);//7


Unlike the Java language, the property of an object in JavaScript can be added or deleted dynamically. When an assignment is made to a property, JavaScript dynamically adds the property in the object if the property does not exist:


Copy Code code as follows:

O.Z = 99;
Console.log (o);//object {x=1, y=7, z=99}

The reading of the property in the prototype inheritance chain

All objects in JavaScript have a prototype prototype object and inherit the property from the prototype object; Therefore, the property of an object in JS is divided into two main categories:

1. The property owned by the object itself ("Own property").
2. Property inherited from a prototype object.

When you read the property of an object, the following rules are followed:

1. Searches for the property that needs to be read from the property collection ("Own property") of the object itself, and if it can be searched, reads the property directly and returns its value.
2. If the property cannot be searched from the object's own property collection ("Own property"), the search continues from the prototype prototype chain of the object until the property is searched and its value is returned.
3. If you cannot search for the property from the property collection ("Own property") of the object itself, and you cannot search for the property from all prototype objects in the object, return undefined.

The writing of the property in the prototype inheritance chain

When writing to the property of a JavaScript object, the following rules are followed:

1. If the object itself has the property, and the property is writable, the new value is written to the property. If the property is read-only, an error occurs.
2. If the object itself does not have the property, and the property is not present in all of its prototype objects, this property is added to the object.
3. If the object itself does not have this property, but its prototype object exists in the property and can be written, then JS will create the property in the object, that is, the object overwrite the property in its prototype object. The property value in the prototype object is unchanged.
4. If the object itself does not have the property, but its prototype object exists in the property and is read-only, an error is provided.
5. If the object itself does not have this property, but its prototype object has a setter method for that property, then JS invokes the setter method in the prototype object. It is important to note that when you run the setter method, if the variable assignment is involved, the assignment action will work on the object itself, and the prototype object will not change. For this behavior, it can be understood that the object inherits the setter function from the prototype and executes it.

As can be seen from the above rules, if the property assignment operation succeeds, then the object itself is always modified, and its prototype prototype object is not changed.

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.