7

One of the strict mode rules (Annex C) states:

When a delete operator occurs within strict mode code, a SyntaxError is thrown if its UnaryExpression is a direct reference to a variable, function argument, or function name.

So in this code:

delete x

x is a reference. (I know this because "the result of evaluating an Identifier is always a value of type Reference"). But is it a direct reference?

And, are there other kinds of references? Indirect references? (If not, what's the point of using the word "direct" at all?)

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
  • "Direct reference" just means a reference without dots or subscript notation. V8 calls it an "unqualified identifier." – Dagg Nabbit Sep 26 '12 at 21:38
  • @GGG Yes, that sounds very reasonable. Also, when `delete x`, Firefox throws *"SyntaxError: applying the 'delete' operator to an unqualified name is deprecated"*, which confirms your statement. – Šime Vidas Sep 26 '12 at 21:45
  • What I'd like to know now is what problem that fixed. I didn't even know you could delete non-properties prior to strict mode. I suppose it could cause some confusion where a long chain of references was concerned if you killed the thing all the references point at. – Erik Reppen Sep 26 '12 at 21:48
  • @ErikReppen `delete` is designed to delete properties. Trying to delete variables, and other kinds of direct references, is improper usage. By default, JavaScript ignores this improper usage, and merely returns `false`. Strict mode, however, throws. When a program uses an operator (like `delete`) improperly, the program should throw - so, strict mode does the right thing. Ideally, this should be the default behavior, but that would break some existing (legacy) programs. That's why this behavior was made opt-in via strict mode. – Šime Vidas Sep 26 '12 at 21:55
  • Ah, okay. And in Chrome at least you can delete global vars in non-strict. – Erik Reppen Sep 26 '12 at 22:34
  • @ErikReppen, I asked that once: http://stackoverflow.com/questions/10643587/ – Dagg Nabbit Sep 26 '12 at 22:57

2 Answers2

1

Yes, there are different kinds of References (EcmaScript §8.7). The member operators (EcmaScript §11.2.1) for example do result in references whose base value is the value of the baseReference, which I'd call "not direct". A "direct reference" would be an identifier reference (EcmaScript §10.2.2.1, where the base value is an Environment Record.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I meant, other kind in regard to a "direct reference". (So, given a direct reference, are there other kinds of references that are not direct references?) That chapter does not mention this term. – Šime Vidas Sep 26 '12 at 21:03
  • Yea, could be. A direct reference would be a reference resolved **solely** via [identifier resolution](http://ecma-international.org/ecma-262/5.1/#sec-10.3.1), whereas an indirect reference would be a property reference (e.g. `obj.prop`), which is resolved in **two** steps: First, identifier resolution is performed on the name `"obj"`, which resolves in a reference to the object, and then, a **new** reference is forged, which base-value is the object-reference, and which name is `"prop"`. Yea, I think that's it. – Šime Vidas Sep 26 '12 at 21:21
0

Anything that's not defined as a property if I understand it correctly.

These should throw errors or fail in a console:

(function(){ 'use strict'; var x = '2'; delete x; })();
(function(){ 'use strict'; delete arguments[0]; })('2');
Erik Reppen
  • 4,605
  • 1
  • 22
  • 26
  • Even when you define a global variable, it will still throw if you refer to it solely by name. For instance: `this.x = true;` (in global code, so `this` is the global object), now `delete x` throws, but `delete this.x` doesn't. So, what matters is how you refer to it in the delete-expression, not weather it's a global property or not. – Šime Vidas Sep 26 '12 at 21:35
  • Yeah I thought I had strict mode in a spot where I didn't when testing in console. Fixed. – Erik Reppen Sep 26 '12 at 21:38