Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

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

>>> var par = {a: 1, b: 2};
undefined
>>> var ch = Object.create(par);
undefined
>>> delete ch.a
true
>>> ch
Object { a=1, b=2}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...