29

JavaScript 声明变量和未声明变量之间的主要区别是什么,因为 delete 运算符不适用于声明的变量?

 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 回答
1

当通过JavaScript中的变量声明创建任何变量时,这些属性是使用“ DontDelete ”属性创建的,这基本上意味着您创建的变量不能使用“delete”表达式删除。默认情况下,所有函数、参数、函数参数都是使用此 DontDelete 属性创建的。您可以将 DontDelete 视为一个标志。

var y = 43;
delete y;         //returns false because it is has a DontDelete attribute

而未声明的分配没有设置任何属性,例如DontDelete。因此,当我们对这个未声明的变量应用删除运算符时,它会返回 true。

x = 42;
delete x;        //returns true because it doesn't have a DontDelete attribute

属性赋值和变量声明之间的区别——后者设置了 DontDelete,而前者没有。这就是未声明的赋值创建可删除属性的原因。

有关删除运算符如何工作的链接

于 2016-02-18T16:35:12.253 回答
0

delete 仅对对象的属性有效。它对变量或函数名称没有影响。

在你的情况下 x = 42; 声明变量 X 并使其成为 Global 对象的属性。所以它返回true。

和 var y = 43; 声明一个不属于任何对象的全局变量,因此它返回 false。

于 2013-04-13T08:39:06.457 回答