The property type of the JavaScript object

Source: Internet
Author: User

ECMA-262 version 5th describes the various characteristics of attributes when defining features that are only used internally. ECMA-262 defines these features for the purpose of implementing JavaScript engines, so they are not directly accessible in JavaScript. To indicate that a feature is an intrinsic value, the specification places them in two opposing brackets, such as [[Enumberable]]. Although the definition of the 3rd edition of ECMA-262 is somewhat different, this book only refers to the 5th version of the description.

There are two types of properties in ECMAScript: data Properties and accessor properties .

Data properties

The Data property contains the location of a data value. Values can be read and written at this location. The data attribute has 4 attributes that describe its behavior.

    • [[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 an accessor property. The default value is true.
    • [[Enumberable]]: Indicates whether the property can be returned through a for-in loop. The default value is true.
    • [[writable]]: Indicates whether the value of the property can be modified. The default value is true.
    • [[Value]]: Contains the data value for this property. When reading a property value, read from this position, and when writing the value of the property, save the new value in this position. The default value is undefined.

Properties that are defined directly on the object, their [[Configurable], [[Enumberable], and [[writable]] attributes are set to true, while the [[Value]] attribute is set to the specified value. For example:

1  var person = {2      name: "Nicholas"    3 };

A property named name is created here, and the value specified for it is "Nicholas". That is, the [[Value]] attribute will be set to "Nicholas", and any modifications to this value will be reflected in this position.

To modify the attributes of a property's default value, you must use the Object.defineproperty () method of ECMAScript 5. This method receives three parameters: the object where the property resides , the name of the property , and a descriptor object . Where the property of the Descriptor object must be: Configurable, enumerable, writable, and value. Set one or more of these values to modify the corresponding attribute values. For example:

1  varperson = {};2Object.defineproperty (person, "name", {3Writable:false,4Configurable:false, 5Value: "Nicholas"6  });7 8alert (person.name);//"Nicholas"9Person.name = "Greg";Tenalert (person.name);//"Nicholas" One  A DeletePerson.name; -alert (person.name);//"Nicholas"
Accessor properties

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 an accessor property. The default value is true.
    • [[Enumberable]]: Indicates whether the property can be returned through a for-in loop. The default value is true.
    • [[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 () . Take a look at the example below.

1 varBook = {2_year:2004,3Edition:14 };5 6Object.defineproperty (book, ' Year ', {7Getfunction() {8         return  This. _year;9     },TenSetfunction(newvalue) { One         if(NewValue > 2004) { A              This. _year =newvalue; -              This. edition + = newValue-2004; -         } the     } - }); -  -Book.year = 2005; +alert (book.edition);//2
Define multiple properties

Using the object.defineproperties () method, this method receives two object arguments: The first object is the object whose properties are to be added and modified, and the properties of the second object correspond to the property one by one to be added or modified in the first object. For example:

1 varBook = {};2 3 object.defineproperties (book, {4 _year: {5Writable:true,6value:20047     },8 Edition: {9Writable:true,TenValue:1 One     }, A Year : { -Getfunction() { -             return  This. _year; the         }, -Setfunction() { -             if(NewValue > 2004) { -                  This. _year =newvalue; +                  This. edition + = newValue-2004; -             } +         } A     } at});

browsers that support the object.defineproperties () method are ie9+, Firefox 4+, Safari 5+, Opera 12+, and Chrome.

Properties of the Read attribute

Using the 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:

1 varBook = {};2 3 object.defineproperties (book, {4 _year: {5Writable:true,6value:20047     },8 Edition: {9Writable:true,TenValue:1 One     }, A Year : { -Getfunction() { -             return  This. _year; the         }, -Setfunction() { -             if(NewValue > 2004) { -                  This. _year =newvalue; +                  This. edition + = newValue-2004; -             } +         } A     } at }); -  - varDescriptor = object.getownpropertydescriptor (book, "_year"); -alert (Descriptor.value);//2004 -alert (descriptor.configurable);//false -AlerttypeofDescriptor.get);//"undefined" in  - varDescriptor = object.getownpropertydescriptor (book, ' Year '); toalert (Descriptor.value);//undefined +alert (descriptor.enumerable);//false -AlerttypeofDescriptor.get);//"function"

Browsers that support the Object.getownpropertydescriptor () method are ie9+, Firefox 4+, Safari 5+, Opera 12+, and Chrome.

The property type of the JavaScript object

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.