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 |
- Indicates whether a property can be redefined by deleting it with delete
- Can I modify attributes of a property
- 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 |
- Indicates whether a property can be redefined by deleting it with delete
- Can I modify attributes of a property
- 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