Adding elements to object

前端 未结 17 2174
有刺的猬
有刺的猬 2020-12-02 04:11

I need to populate a json file, now I have something like this:

{\"element\":{\"id\":10,\"quantity\":1}}

And I need to add another \"elemen

17条回答
  •  一生所求
    2020-12-02 04:34

    This is an old question, anyway today the best practice is by using Object.defineProperty

    const object1 = {};
    
    Object.defineProperty(object1, 'property1', {
      value: 42,
      writable: false
    });
    
    object1.property1 = 77;
    // throws an error in strict mode
    
    console.log(object1.property1);
    // expected output: 42
    

提交回复
热议问题