Why delete operator returns true even if property wasn't deleted from an object

后端 未结 1 992
南旧
南旧 2020-12-07 03:59
>>> var par = {a: 1, b: 2};
undefined
>>> var ch = Object.create(par);
undefined
>>> delete ch.a
true
>>> ch
Object { a=1, b=2}
<         


        
相关标签:
1条回答
  • 2020-12-07 04:56

    You misunderstood what delete returns:

    Throws in strict mode if the property is an own non-configurable property (returns false in non-strict). Returns true in all other cases. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete)

    delete ch.a tries to locate the property a in ch, fails (since ch doesn't have such own property), does nothing and happily returns true. If you wrote delete ch.foobar, the result would be the same. If however, you tried a non-configurable property (e.g. delete ch.__proto__), the result would be false.

    0 讨论(0)
提交回复
热议问题