[removed] constant properties

后端 未结 4 2018
独厮守ぢ
独厮守ぢ 2021-02-13 20:59

In javascript, can I declare properties of an object to be constant?

Here is an example object:

   var XU = {
      Cc: Components.classes
   };
<         


        
4条回答
  •  一整个雨季
    2021-02-13 21:40

    To define a constant property, you could set the writable attribute to false in the defineProperty method as shown below:

    Code snippet:

    var XU = {};
    
    Object.defineProperty(XU, 'Cc', {
        value: 5,
        writable: false
    });
    
    XU.Cc = 345;
    
    console.log(XU.Cc);
    

    Result:

    5           # The value hasn't changed
    

提交回复
热议问题