JavaScript accessor property

 

 

A visit properties:

  1, Configurable: delete by deleting attribute indicating whether to redefine the attributes can modify the characteristics of the properties, or can modify the attribute data attribute. For attributes defined directly on the object, the default attribute is true, that may be used to delete the delete attribute; properties for use Object.defineProperty manner defined, the default value of this characteristic is false, indicating delete can not be used to remove the property.

  For example:

var obj = {}; 
Object.defineProperty (obj, 'name' , { 
    value: 'Lili' 
}); 
obj.age = '23 ' ; 

Delete obj.name; // the return value of false, given the strict mode, property is not properly removed. 
the Delete obj.age; // returns the value true, the attribute can be removed successfully.

  2, Enumerable: by showing whether for - in loops back property. For attributes defined directly on the object, the default attribute is true, that may be used for - in loops back properties; for use Object.defineProperty define attributes, the default value of this property to false, indicating not be used for - in circulation to return property.

for (One in obj) { 
    the console.log (One + ":" + obj [One]); 
} 
// results do not contain the results of the use of the name attribute defined Object.defineProperty 
// Age: 23 is

  3.get: function called when reading the property. The default value is undefined.

  4.set: function called when writing property. The default value is undefined.

// following a similar two-way binding, for example, showed that assigns them when and how to use the set and get methods. 
var obj = { 
    TEMP: '' , 
    Array: [], 
    Push: function (Val) {
         the this .temp = Val; 
    } 
}; 


Object.defineProperty (obj, 'TEMP' , { 
    SET: function (Val) { 
        obj. Array.push (Val); 
    }, 
   GET: function () {
      return obj.array; 
   } 
}); 

obj.push ( "pro" ); 
obj.push ( "love" ); 
obj.push ( "a"); 
Obj.push ( "you"); 
// result
obj.array; // get directly from the object
obj.temp; // go get method

 

 




 

Guess you like

Origin www.cnblogs.com/pecool/p/11484671.html