JavaScript Advanced Programming Reading notes (1)

Source: Internet
Author: User

the 6th Chapter object-oriented ProgrammingTypes of Properties

There are two properties of objects in JS, data properties and accessor properties (accessor property), attributes of which have their own properties (Arrribute), which can be understood as properties about attributes.

The data attribute contains 4 attributes, [[[Configurable]], [[Enumerable]], [[writable]], [[Value]].

When you use Object.defineproperty to modify a property descriptor, if the property does not exist before it, except for the attribute value specified in the code, the other attribute value defaults to False, for example:

Let obj = {};obj.name = ' test '; Object.defineproperty (obj, ' age ', {    writable:false}); O Bject.getownpropertydescriptor (obj, ' age ');//console output information is as follows: Note Four attribute values are falseobject {value:undefined, Writable:false, Enumerable:false, Configurable:false}

Accessor properties cannot be defined directly and must be defined using Object.defineproperty, which contains 4 attributes, [[configurable]], [[Enumerable]], [[Get]], [[Set]]. Examples are as follows:

//Common ways to use accessor properties: Setting the value of one property causes other properties to change 
Let book = 2004 1};object.defineproperty (book, ' year ' function () { return this ._year}, set: function (newvalue { if (Newvalue>2004 this . _year = NewValue; this . Edition + = Newvalue-2004; } }});

Although we can also modify this property directly through Book._year access, the underscore in front of _year is a common notation that represents a property that can only be accessed through an object method, noting that this is a convention rather than a limitation of the language itself.

JavaScript Advanced Programming Reading notes (1)

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.