Object accessor properties in ECMAScript5: Getter and setter Introduction _javascript Tips

Source: Internet
Author: User

Obviously this is a topic that has nothing to do with IE (except advanced IE), however, interested students should come together to realize the getter and setter in the ECMASCRIPT5 standard. In an object, the operation of the property or method, usually the most use is read (reference) and write, such as O.get, this is a read operation, and O.set = 1 is a write operation. In fact, the key values of any object can be replaced by getter and setter methods in the implementation of the latest mainstream browsers except IE, which is called accessor properties.

There is no doubt that the getter is responsible for querying the value, it takes no parameters, the setter is responsible for setting the key value, the value is passed in the form of parameters, in the body of his function, all return is invalid. Unlike normal attributes, the memory attribute is not available for both read and write when only a get or set is declared, and when it has only the Getter method, it is only read-only, and when it has only a setter method, you will always read undefined. How do I declare object memory properties? The quickest way to do this is to use the syntax of the object literal to write, see the following code:

Copy Code code as follows:

var oo = {
Name: ' Hyun-xin ',
Get Sex () {
Return to ' man ';
}
};
Obviously this is not allowed, because the yin heart does not want the outside world to change the fact that he is a man, so for sex only set the read-only function
Oo.sex = ' woman ';
Console.log (Oo.sex); The result is still man.

Interestingly, this overturns our previous understanding that the Function keyword was not used in the method definition. In fact, the get or set here, you can understand the function in two different states: the tolerant side (writing), The safe side (reading), when a whole is dismembered into different forms, means that we may no longer need to follow the tradition in the form of expression, so we do not use a colon to separate the keys from the values. So, continue with the example above. How will you be able to read and write on the basis of the memory attributes, perhaps the following paragraph will give you an answer:

Copy Code code as follows:

var oo = {
Name: ' Hyun-xin ',
Get Sex () {
if (This.sexx) {
return This.sexx;
}else{
Return to ' man ';
}
}, set Sex (val) {
This.sexx = val;
}
};
Oh, he's so tolerant, and even people change his sex, and he accepts
Oo.sex = ' woman ';
Console.log (Oo.sex); Results woman

You might think this is superfluous, because we can totally ignore get and set, and directly let the sex method have two kinds of permissions. But the reason we take get and set out alone is to understand more clearly the ECMAScript5 of JavaScript object key operations, a more rigorous interpretation. Of course, in IE contaminated China, the new mainstream technology always seems to be out of tune, in the actual project development, you may never use get and set, but who can guarantee that later?

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.