How to create a JS object with the default prototype from an object without a prototype?

前端 未结 4 2231
傲寒
傲寒 2021-01-19 22:58

Background: The module query-string is for example able to parse key=value&hello=universe to an object {key: \'value\', hello: \'univ

4条回答
  •  醉酒成梦
    2021-01-19 23:42

    I can't say I've ever done this before, but here's one way to do it

    let bastard = Object.create(null);
    bastard.father = 'vader';
    
    let adopted = Object.assign({}, bastard);
    console.log(adopted.hasOwnProperty('father')); // => true

提交回复
热议问题