JavaScript--Understanding the properties of an object

Source: Internet
Author: User

Note: This article has been modified longDescription: There are two types of properties in ECMAScript: Data properties and accessor properties. Property Descriptor 1. Data Property Description: When modifying or defining a property of an object, add some attributes to the property.
attribute name Description Default Value
Value Set the value of a property Undefined
Writable Set whether a value can be modified True
Enumerable Indicates whether the property can be returned through a for-in or Obj.keys () loop. True
Configurable
  1. Indicates whether a property can be redefined by deleting it with delete
  2. Can I modify attributes of a property
  3. Can I modify properties to accessor properties
True
varObj= {    Test:"Hello"}//Object existing attribute added attribute descriptionObject.DefineProperty(obj,"Test",{    Configurable:true | false,    Enumerable:true | false,    value:Any type of value,    writable:true | false});the attribute description of the newly added property of the//objectObject.DefineProperty(obj,"NewKey",{    Configurable:true | false,    Enumerable:true | false,    value:Any type of value,    writable:true | false});

To Object.defineProperty Object.defineProperties add data properties with or without a function, the Object.create writable, enumerable, and configurable default values are false .

Properties created using object literals , the writable, enumerable, and configurable attributes default to True.

2. Accessor Property Description: Sets or gets the value of a property of an object.
attribute name Description Default Value
Get Functions that are called when a property is read Undefined
Set Functions that are called when a property is written True
Enumerable Whether it can be enumerated (using for...in or Object.keys ()) True
Configurable
  1. Indicates whether a property can be redefined by deleting it with delete
  2. Can I modify attributes of a property
  3. Can I modify the property to an accessor property (whether the target property can set the attribute again)
True
var obj = {};Object.defineProperty(obj, "newKey", {    get:function (){} | undefined,    set:function (value){} | undefined    configurable: true | false    enumerable: true | false});
Note: When a getter or setter method is used, it is not allowed to use the writable and Value properties ECMAScript 5 to manipulate property attributes in the method: 1. Object.defineproperty () Description: This method can only define one property

Grammar:

Object.defineProperty(obj, prop, descriptor)

Parameter list:

obj:必需。目标对象 prop:必需。需定义或修改的属性的名字descriptor:必需。目标属性所拥有的特性

return value:

传入函数的对象。即第一个参数obj
Example
varObj= {};//Object.defineproperty (object, property, property descriptor)Object.DefineProperty(obj, "Name", {    Get: function(){        return  This._name;  //Use the access attribute in get and set must add "_"    },    Set: function(Val){        if(Array.IsArray(Val)){             This._name =Val;        } Else{             This._name = "Not an array cannot be assigned a value";        }    },    Enumerable: true,       //Represents an enumerable    Configurable: true      //Whether the property can be deleted});//Object {get:function, set:function, Enumerable:true, configurable:true}Console.Log(Object.Getownpropertydescriptor(obj, ' name ')); obj.name = "111";Console.Log(obj.name);
2. Object.defineproperties () Description: This method is used to define multiple properties.
varObj= {};Object.defineproperties(obj, {    name: {        value: "Emil Chau",        writable: true    },     Age: {        value:  -,        writable: true    },    Sex: {        Get: function(){            return  This._sex;        },        Set: function(Val){            if(Val=== 1){                 This._sex = "Male";            }Else{                 This._sex = "female";            }        }    }});obj.Sex = 0;Console.Log(obj.Sex); //female

JavaScript--Understanding the properties of an 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.