6.1.3 Properties of Read attributes

Source: Internet
Author: User

Using the ECMAScript5 Object.getownpropertydescriptor () method, you can obtain a descriptor for a given property. This method receives two parameters: the object where the property resides and the name of the property whose descriptor you want to read. The return value is an object, and if it is an accessor property, the property of the object has configurable, enumerable, get, and set, and if it is a data property, the object's properties are configurable, enumerable, Writable and value. For example:

var book = {};object.defineproperties (book,{    _year:{        value:2004    },        edition:{        value:1    },    year:{        get:function () {            return this._year;        },         set:function (newvalue) {            if (newvalue> 2004) {                this._year = newvalue;                This.edition + = newValue-2004;}}}    ); var descriptor = object.getownpropertydescriptor (book, "_year"); alert (descriptor.value);//2004alert ( descriptor.configurable);//falsealert (typeof descriptor.get);//"undefined" var descriptor = Object.getownpropertydescriptor (book, ' Year '); alert (descriptor.value);//undefinedalert (descriptor.enumerable); /falsealert (typeof descriptor.get);//"function"

For data Properties _year,value equals the original value, configurable is false, and get equals undefined. For accessor properties Year,value equals undefined,enumerable is false, and get is a pointer to the Getter function.

In JavaScript, you can use the Object.getownpropertydescriptor () method for any object, including Dom and BOM objects. Browsers that support this approach are ie9+, ff4+, safari5+, opera12+, and Chrome.

6.1.3 Properties of Read attributes

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.