已申报和未申报变量的影响 [英] Effect of declared and undeclared variables
问题描述
JavaScript声明变量和未声明变量之间的主要区别是什么,因为
删除操作符不适用于声明变量?
var y = 43; //声明一个新变量
x = 42;
删除x; //返回true(x是全局对象的一个属性,可以删除)
delete y; //返回false(删除不影响变量名称)
为什么会发生这种情况?全局声明的变量也是窗口对象的属性,为什么它不能被删除?
声明和未声明的全局变量
存储和访问它们的机制是相同的,但JavaScript在某些情况下会根据可配置
属性(如下所述)。在常规用法中,它们应该表现相同。
全局对象都存在
以下是一些比较 和 undeclared 全局变量。
//显式全局变量(新变量)undeclared = 1; //隐式全局变量(默认全局对象的属性)
window.hasOwnProperty('声明')// true
window.hasOwnProperty('undeclared')// true
window.propertyIsEnumerable('declared')// true
window.propertyIsEnumerable('undeclared')// true
window.declared // 1
窗口。未申报的// 1
window.declared = 2;
window.undeclared = 2;
声明// 2
未声明// 2
删除声明// false
删除未声明// true
删除未声明// // true(同样的结果,如果再次删除)
delete window.declared // false
delete window.undeclared // true(同样的结果,如果再次删除它)
删除窗口.undeclared // true(仍然为true)
对于声明的全局变量, 如果属性的 在非严格模式, 声明的全局变量 未声明的全局变量 What is the major difference between JavaScript declared and undeclared variables, since
the delete operator doesn't work on declared variables? Why does this happen? Variables declared globally are also the properties of the window object, so why can't it be deleted? The mechanism for storing and accessing them is the same, but JavaScript treats them differently in some cases based on the value of the Below are some comparisons of declared and undeclared global variables. Both declared and undeclared global variables are properties of the For declared global variables, the If the In non-strict mode, the Declared global variable Undeclared global variable 这篇关于已申报和未申报变量的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
和未声明的全局变量是窗口
对象(默认全局对象)的属性。原型链中的任何一个都不是通过不同的对象继承的。它们都直接存在于窗口
对象中(因为 window.hasOwnProperty
返回 true
/ p>
可配置属性
可配置的
属性是 false
。对于未声明的全局变量,它是 true
。 可配置
属性的值可以使用 getOwnPropertyDescriptor
的方法,如下所示:
var declared = 1;
undeclared = 1;
(Object.getOwnPropertyDescriptor(window,'声明'))。可配置// false
(Object.getOwnPropertyDescriptor(window,'undeclared'))。可配置// true
可配置
属性为true,可以使用 defineProperty
方法,并且可以使用 delete
运算符。否则,属性无法更改,并且无法以此方式删除属性。
delete
运算符返回 true
如果属性是可配置的,并且如果它是不可配置的,则返回 false
。
< h2>总结
$删除b
window )的属性
删除
运算符
$ b
window
)
<李>的能使用删除
运算符
删除 >
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)
Declared and undeclared global variables
configurable
attribute (described below). In regular usage, they should behave the same.Both exist in the global object
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)
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
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
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.delete
operator returns true
if the property is configurable, and returns false
if it's non-configurable.Summary
window
)delete
operator
window
)delete
operatorSee also