6.1.1.2 accessor properties of a property type

Source: Internet
Author: User

Accessor properties do not contain data values; they contain a pair of getter and setter functions (however, neither of these functions is required). When the accessor property is read, the Getter function is called, which is responsible for returning a valid value, and when writing the accessor property, the setter function is called and the new value is passed, which determines how the data is processed. Accessor properties have the following 4 attributes.

    • [[Configurable]]: Indicates whether the property can be redefined by deleting the property from Delete, can modify the attribute's attributes, or can modify the property to a data property. The default value for this attribute is true for properties that are defined directly on the object.
    • [[Enumerable]]: Indicates whether the property can be returned through a for-in loop. The default value for this attribute is true for properties that are defined directly on the object.
    • [[Get]]: The function that is called when the property is read. The default value is undefined.
    • [[Set]]: The function that is called when the property is written. The default value is undefined.

Accessor properties cannot be defined directly and must be defined using Object.defineproperty (). For example:

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 = 2005;alert (book.edition);//2

The above code creates a book object and defines two default properties for it: _year and edition. _year the preceding underscore is a common notation used to represent properties that can be accessed only through object methods. The accessor property, year, consists of a getter function and a setter function. The Getter function returns the value of _year, and the setter function determines the correct version by calculation. Therefore, modifying the Year property to 2005 causes _year to become 2005, and edition becomes 2. This is a common way of using accessor properties, where setting the value of one property causes other properties to change.

Do not necessarily have to develop getter and setter at the same time. Specifying the getter only means that the property cannot be written, and the attempt to write the property is ignored. In strict mode, attempting to write a property that specifies only the Getter function throws an error. Similarly, properties that specify only setter functions are also unreadable, otherwise undefined are returned in non-strict mode, and errors are thrown in strict mode.

Browsers that support this method of ECMAScript 5 have ie9+ (IE8 only partially implemented), ff4+, safari5+, opera12+, and Chrome. Before this approach, to create accessor properties, you typically use two standard methods: _definegetter_ () and _definesetter_ (). These two methods were originally introduced by Firefox, and later Safari3, Chrome1, and Opera9.5 also gave the same implementation. Using these two legacy methods, you can rewrite the previous example as follows.

var book ={    _year=2004,    edition:1};//The old method of defining accessors Book._definegetter_ ("Year", function () {    return this._ Year;}); Book._definesetter_ ("Year", function () {    if (newvalue>2004) {        this._year = newvalue;        This.edition + = newValue-2004;    }}); Book.year=2005;alert (book.edition);//2

[[Configurable] and [[Enumerable]] cannot be modified in browsers that do not support the Object.defineproperty () method.

6.1.1.2 accessor properties of a property type

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.