29

削除演算子は宣言された変数では機能しないため、JavaScript で宣言された変数と宣言されていない変数の主な違いは何ですか?

 var y = 43;     // declares a new variable
 x = 42;

 delete x;       // returns true  (x is a property of the global object and can be deleted)
 delete y;       // returns false (delete doesn't affect variable names) 

なぜこれが起こるのですか?グローバルに宣言された変数は window オブジェクトのプロパティでもあるのに、なぜ削除できないのでしょうか?

4

4 に答える 4

30

Declared and undeclared global variables

The mechanism for storing and accessing them is the same, but JavaScript treats them differently in some cases based on the value of the configurable attribute (described below). In regular usage, they should behave the same.

Both exist in the global object

Below are some comparisons of declared and undeclared global variables.

var declared = 1;  // Explicit global variable (new variable)
undeclared   = 1;  // Implicit global variable (property of default global object)

window.hasOwnProperty('declared')    // true
window.hasOwnProperty('undeclared')  // true

window.propertyIsEnumerable('declared')    // true
window.propertyIsEnumerable('undeclared')  // true

window.declared     // 1
window.undeclared   // 1

window.declared   = 2;
window.undeclared = 2;

declared     // 2
undeclared   // 2

delete declared     // false
delete undeclared   // true
delete undeclared   // true (same result if delete it again)

delete window.declared     // false
delete window.undeclared   // true (same result if delete it yet again)
delete window.undeclared   // true (still true)

Both declared and undeclared global variables are properties of the window object (the default global object). Neither one is inherited from a different object through the prototype chain. They both exist directly in the window object (since window.hasOwnProperty returns true for both).

The configurable attribute

For declared global variables, the configurable attribute is false. For undeclared global variables, it's true. The value of the configurable attribute can be retrieved using the getOwnPropertyDescriptor method, as shown below.

var declared = 1;
undeclared = 1;

(Object.getOwnPropertyDescriptor(window, 'declared')).configurable     // false
(Object.getOwnPropertyDescriptor(window, 'undeclared')).configurable   // true

If the configurable attribute of a property is true, the attributes of the property can be changed using the defineProperty method, and the property can be deleted using the delete operator. Otherwise, the attributes cannot be changed, and the property cannot be deleted in this manner.

In non-strict mode, the delete operator returns true if the property is configurable, and returns false if it's non-configurable.

Summary

Declared global variable

  • Is a property of the default global object (window)
  • The property attributes cannot be changed.
  • Cannot be deleted using the delete operator

Undeclared global variable

  • Is a property of the default global object (window)
  • The property attributes can be changed.
  • Can be deleted using the delete operator

See also

于 2013-04-15T03:43:09.887 に答える
1

主な違いは、関数内で変数を宣言するときです。関数内で変数を宣言するときに使用varすると、その変数はローカル変数になります。ただし、 を使用しない場合はvar、変数を宣言する場所 (関数の内外) に関係なく、変数はグローバル変数になります。

于 2013-04-13T08:38:51.347 に答える
0

delete は、オブジェクトのプロパティに対してのみ有効です。変数名や関数名には影響しません。

あなたの場合 x = 42; 変数 X を宣言し、グローバル オブジェクトのプロパティにします。したがって、true が返されます。

そして var y = 43; オブジェクトの一部ではないグローバル変数を宣言するため、false を返します。

于 2013-04-13T08:39:06.457 に答える