undefinedだけじゃないのか、デフォルトのグローバルオブジェクト

ECMA-262を読んでいたら、たまたま見つけたのでメモ。いつもNumber.NaNとか使ってた。

15.1.1 Value Properties of the Global Object

15.1.1.1 NaN
The initial value of NaN is NaN (8.5). This property has the attributes { DontEnum, DontDelete}.

15.1.1.2 Infinity
The initial value of Infinity is +∞ (8.5). This property has the attributes { DontEnum,
DontDelete}.

15.1.1.3 undefined
The initial value of undefined is undefined (8.1). This property has the attributes { DontEnum, DontDelete }.

{DontDelete}というのはdeleteキーワードの挙動を示している。

var a = 'abc';
delete a;
alert(a); // 'abc'

with ({a: null}) {
  var a = 'def';
  alert(a); // 'def'
  delete a;
  alert(a); // 'abc'
  delete a;
  alert(a); // 'abc'
}

(function() {
  var a = 'ghi';
  alert(a); // 'ghi'
  delete a;
  alert(a); // 'ghi';
})();

ちなみに代入はできるですね。

JavaScript で undefined って予約語じゃなかったんだ!!