Getter and setter methods for attributes in JavaScript objects __java

Source: Internet
Author: User

The properties of a JavaScript object consist of a name, a value, and a set of attributes (writable, enumerable, configurable, and so on). In ECMAScript 5, property values can be replaced by one or two methods, which are getter and setter.

var myobj = {
    a:2, get
    B () {return
        3;
    }   
};

Console.log (MYOBJ.A);//2
Console.log (myobj.b);//3

In the code above, property A is called a " Data property ", and it has only a simple value; properties such as Property B, which are defined with getter and setter methods, are called accessor properties .

Accessor properties are defined as one or two functions that have the same name as a property, which does not use the Function keyword, but uses get or set, and does not use a colon to separate the property name from the function body, but the end of the function body and the next method are separated by commas.

When the program queries the accessor's property value, the JavaScript substitution getter method (without parameters), the return value of this method is the value of the property's access expression. When the program sets an accessor property value, JavaScript invokes the setter method, passing the value on the right side of the assignment expression as a parameter to the setter. In a sense, this method is responsible for setting the property value and can ignore the return value of the method.

When a property is defined as an accessor property, JavaScript ignores its value and writable attributes and replaces the set and get (and configurable and enumerable) attributes.

var myobj = {get
    a () {return
        2;
    }   
};

MYOBJ.A = 3;

Console.log (MYOBJ.A);//2

As shown in the preceding code, because we define only the getter of property A, set will ignore the assignment operation and not throw an error when setting a (that is, assigning) a value.

var myobj = {get
    a () {return
        this._a_;
    },
    set a (val) {
        this._a_ = val;
    }   
};

MYOBJ.A = 3;

Console.log (MYOBJ.A);//3

The correct wording is as shown above. This in the getter and setter methods points to the MyObj object. Here we store 3 of the assignment operations in another intermediate variable _a_. Name _a_ is just a convention, and there is no other special behavior, it is just a common attribute. Replace it with any other legal name, such as _b_, which can even be printed out on the outside.

var myobj = {get
    a () {return
        this._b_;
    },
    set a (val) {
        this._b_ = val;
    }   
};

MYOBJ.A = 3;

Console.log (MYOBJ.A);//3
Console.log (myobj._b_);//3

In addition, accessor properties can also be inherited:

var myobj = {get
    a () {return
        this._b_;
    },
    set a (val) {
        this._b_ = val;
    }   
};

MYOBJ.A = 3;

var anotherobj = object.create (myobj);
Console.log (ANOTHEROBJ.A);//3
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.