当前位置:网站首页>Implement const in Es5

Implement const in Es5

2022-06-25 23:56:00 wen_ Wen Wen

ES5 Two attribute types in : Data properties and accessor properties ;

Data attribute : Contains the location of a data value , In this location, you can read and write values ; Yes 4 A characteristic that describes its behavior ;

Configuarable: Can we pass delete Delete attribute , And redefine the properties ,

Enumerable: Can we pass for-in Loop returns the property ,

Writable: Whether the value of the property can be modified ;

Value: The data value of this property , The default is undefined

Depending on how you define objects ,Configurable,Enumerable,Writable The default values are also different

The way 1: var person = {name: ' Zhang San '}

be person Object's name Property in the property descriptor object of the Configurable,Enumerable,Writable All values default to true

The way 2:

var person = {}

Object.defineProperty(person,'name',{

value: ' Zhang San '

})

be person Object's name Property in the property descriptor object of the Configurable,Enumerable,Writable All values default to false

    // "use strict"
    var person = {}
    Object.defineProperty(person, 'name', {
      value: ' Zhang San '
    })
    console.log(person.name); // Zhang San 
    // When strict mode is enabled : "use strict"; Uncaught TypeError: Cannot assign to read only property 'name' of object '#<Object>'
    person.name = ' Li Si '; 
    console.log(person.name);   // Zhang San 

Accessor properties : Does not contain data values , Containing a pair of getter and setter function

Configuarable: Can we pass delete Delete attribute , And redefine the properties ,

Enumerable: Can we pass for-in Loop returns the property ,

Get: Function called when reading properties , The default is undefined

Set: The function called when the property is written , The default is undefined

Accessor properties cannot be defined directly , You have to use Object.defineProperty() To define

 

// 1.  Custom containers act as storage for data 
    var constObj = {}
    function getConst(key, val) {
      constObj[key] = val;
      Object.defineProperty(constObj, key, {
        value: val,
        configurable: false, // Whether the configuration information can be modified 
        writable: false, // Whether the attribute value can be modified 
        enumerable: true // Enumerable or not 
      })
    }
    getConst('a', 10)
    constObj.a = 20;
    console.log(constObj.a);    //10

   // 2.  Mount to window above 
    function getConst2(constKey, constVal) {
      window[constKey] = constVal;
      Object.defineProperty(window, constKey,{
        get: function() {   // If set  set  or  get,  You can't set  writable  and  value  Any one of them , Otherwise, the report will be wrong 
          return constVal;
        },
        enumerable: true, // Enumerable or not 
        configurable: false,  // Whether the configuration information of the current description can be modified 
      })
    }
    getConst2('b', 30);
    getConst2('c', 50);
    console.log(window.b);    //30
    console.log(window.c);    //50
    window.b = 66
    console.log(window.b);    //30

 

原网站

版权声明
本文为[wen_ Wen Wen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252102465936.html