how to use javascript Object.defineProperty

前端 未结 10 1189
不知归路
不知归路 2020-11-29 14:21

I looked around for how to use the Object.defineProperty method, but couldn\'t find anything decent.

Someone gave me this snippet of code:

Object.def         


        
10条回答
  •  再見小時候
    2020-11-29 14:56

    Defines a new property directly on an object, or modifies an existing property on an object, and return the object.

    Note: You call this method directly on the Object constructor rather than on an instance of type Object.

       const object1 = {};
       Object.defineProperty(object1, 'property1', {
          value: 42,
          writable: false, //If its false can't modify value using equal symbol
          enumerable: false, // If its false can't able to get value in Object.keys and for in loop
          configurable: false //if its false, can't able to modify value using defineproperty while writable in false
       });
    

    Simple explanation about define Property.

    Example code: https://jsfiddle.net/manoj_antony32/pu5n61fs/

提交回复
热议问题