JavaScript Accessor Property Instance analysis _javascript techniques

Source: Internet
Author: User

This article analyzes the usage of JavaScript accessor properties and shares them for everyone's reference. The specific analysis is as follows:

This is a bit like the meaning of the constructor, but the function is different, he can have two properties to establish an association, by modifying a property to change another property.

Copy Code code as follows:
var book = {
_year:2004,
Edition:1
};
Object.defineproperty (book, year), {
Get:function () {
return this._year;
},
Set:function (NewValue) {
if (NewValue > 2004) {
This._year + = newvalue;
This.edition + = newValue-2004;
}
}
});
Book.year = 2006;
Console.log (book.edition);

As can be seen from the example, modifying _year, the output value changes only year, but through set, the edition changes at the same time when the year is modified.

I hope this article will help you with your JavaScript programming.

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.