SoFunction
Updated on 2025-03-10

Object accessor properties in ECMAScript5: Getter and setter introduction

Obviously this is a topic that is irrelevant to IE (except for advanced IE). Despite this, interested students should learn about the implementation of getters and setters in the ECMAScript5 standard. In an object, the most commonly used operation of properties or methods is read (reference) and write. For example, this is a read operation, and = 1 is a write operation. In fact, in the implementation of the latest mainstream browsers except ie, the key value of any object can be replaced by getter and setter methods, which is called "accessor attribute".

There is no doubt that getter is responsible for querying the value, it does not take any parameters, and setter is responsible for setting the key value. The value is passed in the form of parameters. In its function body, all return is invalid. Unlike ordinary properties, when only get or set is declared, the memory property cannot have both read and write. When it only has a getter method, it is only read. Similarly, when it only has a setter method, then what you read is always undefined. How to declare object memory attributes? The fastest way is to use the syntax of object literals to write. Please see the following code:

Copy the codeThe code is as follows:

var oo = {
name: 'Xianxin',
    get sex(){
        return 'man';
    }
};
//Obviously this is not allowed, because Xianxin does not want the outside world to change the fact that he is a man, so only the read-only function is set for sex.
= 'woman';
(); //The result is still man

Interestingly, this subverts our previous understanding, that is, the function keyword is not used when defining the method. In fact, get or set here, you can understand it as a function in two different states: the inclusive side (write), the safe side (read), when a whole is dismembered into a different form, it means that we may no longer need to follow tradition in terms of expression, so we do not use colons to separate keys and values. Then, continue with the example above. How will you become both read and write based on memory properties? Maybe the following paragraph will give you the answer:

Copy the codeThe code is as follows:

var oo = {
name: 'Xianxin',
    get sex(){
        if(){
            return ;
        }else{
            return 'man';
        }
    }, set sex(val){
        = val;
    }
};
//Oh, he is so tolerant that even people change his gender, he accepts it.
= 'woman';
(); //Result woman

Maybe you will think this is unnecessary, because we can completely ignore get and set and directly allow the sex method to have two permissions. But the reason why we took out get and set separately is to understand more clearly the key-value operation of ECMAScript5 on JavaScript object, a more rigorous interpretation. Of course, in China polluted by IE, new mainstream technologies always seem out of place. In actual project development, you may never use get and set, but who can guarantee that you won’t do it in the future...