最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In JavaScript, what happens if "delete" a property that doesn't exist? - Stack Overflow

programmeradmin5浏览0评论

What happens in JavaScript if I have a variable, say:

var exampleObject = {one:'foo',two:'bar'};

and then I delete a property that doesn't exist, a la:

delete exampleObject.seven;

Is there a standard course of action that takes place everywhere (nothing, error message, script crashes, etc.), or is this dependent on some kind of implementation (browser engine, etc.)?

What happens in JavaScript if I have a variable, say:

var exampleObject = {one:'foo',two:'bar'};

and then I delete a property that doesn't exist, a la:

delete exampleObject.seven;

Is there a standard course of action that takes place everywhere (nothing, error message, script crashes, etc.), or is this dependent on some kind of implementation (browser engine, etc.)?

Share Improve this question asked May 6, 2013 at 20:31 thisissamithisissami 16.3k16 gold badges48 silver badges77 bronze badges 4
  • 9 Is it that hard to test? – Joseph Commented May 6, 2013 at 20:31
  • 26 It's not obvious to me whether or not something that takes place is dependent on the environment or a standard across all implementations of the language. – thisissami Commented May 6, 2013 at 20:33
  • 7 the world ends in an explosion of god's tears – nathan hayfield Commented May 6, 2013 at 20:34
  • 1 developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/… – Tom Hubbard Commented May 6, 2013 at 20:34
Add a comment  | 

2 Answers 2

Reset to default 49

Nothing happens.

Assuming, x = {}, Type(x.y) is not a Reference Specifcation Type (there cannot be a "reference" to a property that does not exist). According to 11.4.1 The delete Operator, this satisfies the rule:

  1. Let ref be the result of evaluating UnaryExpression.
  2. If Type(ref) is not Reference, return true.
  3. ...

This behavior (of "no action") has existed for a long time - any environment that behaves differently is non-compliant. From the 3rd Edition ECMAScript Specification:

When the [[Delete]] method of O is called with property name P, the following steps are taken:

  1. If O doesn’t have a property with name P, return true.
  2. ..

If exampleObject is an object, the return value from delete is true, even if the property does not exist.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论