ECMAScript5 new object syntax sugar getter and setter

Source: Internet
Author: User

In the new ECMAScript5, two new syntactic sugars have been added, and these two syntactic sugars are the same.

var obj = (function() {    var num = ten;     return  {
get N () { return num; }, set N (v) { = v; } =Console.log (OBJ.N);

Get means to read a value, in fact the implementation of get and set is a function, note that these two methods can only be used in the object, the other need to: remove, previously we write object properties need to add a colon, using these two methods do not need to add.

N in the above code is not fixed, write casually.

What's the use?

You can also think of no use, it just provides a new way of writing, for example, we have previously obtained the value of the function is required to add (), with this we can and get an object of the same property.

It is also important to note that the set ignores the return value because set is only used to change a value, and return does not make much sense.

ECMAScript5 new object syntax sugar getter and setter

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.