javascript Effect of declared and undeclared variables
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15985875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Effect of declared and undeclared variables
提问by Maizere Pathak.Nepal
What is the major difference between JavaScript declared and undeclared variables, since the delete operator doesn't work on declared variables?
What is the major difference between JavaScript declared and undeclared variables, since the delete operator doesn't work on declared variables?
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)
Why does this happen? Variables declared globally are also the properties of the window object, so why can't it be deleted?
Why does this happen? Variables declared globally are also the properties of the window object, so why can't it be deleted?
采纳答案by Matt Coughlin
Declared and undeclared global variables
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.
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
Both exist in the global object
Below are some comparisons of declaredand undeclaredglobal variables.
Below are some comparisons of declaredand undeclaredglobal 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 declaredand undeclaredglobal 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).
Both declaredand undeclaredglobal 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
The configurable attribute
For declaredglobal variables, the configurable
attribute is false
. For undeclaredglobal variables, it's true
. The value of the configurable
attribute can be retrieved using the getOwnPropertyDescriptor
method, as shown below.
For declaredglobal variables, the configurable
attribute is false
. For undeclaredglobal 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.
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.
In non-strict mode, the delete
operator returns true
if the property is configurable, and returns false
if it's non-configurable.
Summary
Summary
Declared global variable
Declared global variable
- Is a property of the default global object (
window
) - The property attributes cannotbe changed.
- Cannotbe deleted using the
delete
operator
- Is a property of the default global object (
window
) - The property attributes cannotbe changed.
- Cannotbe deleted using the
delete
operator
Undeclared global variable
Undeclared global variable
- Is a property of the default global object (
window
) - The property attributes canbe changed.
- Canbe deleted using the
delete
operator
- Is a property of the default global object (
window
) - The property attributes canbe changed.
- Canbe deleted using the
delete
operator
See also
See also
回答by Sasank Sunkavalli
When any variable is created via Variable Declaration in JavaScript, these properties are created with "DontDelete" attribute , which basically means that variable you created cannot be Deleted using "delete" expression. All the functions, arguments , function parameters by default are created with this DontDelete attribute. You can think of DontDelete as a flag.
When any variable is created via Variable Declaration in JavaScript, these properties are created with "DontDelete" attribute , which basically means that variable you created cannot be Deleted using "delete" expression. All the functions, arguments , function parameters by default are created with this DontDelete attribute. You can think of DontDelete as a flag.
var y = 43;
delete y; //returns false because it is has a DontDelete attribute
Whereas Undeclared assignment doesn't set any attributes like DontDelete. So when we apply deleteoperator on this undeclared variable , it returns true.
Whereas Undeclared assignment doesn't set any attributes like DontDelete. So when we apply deleteoperator on this undeclared variable , it returns true.
x = 42;
delete x; //returns true because it doesn't have a DontDelete attribute
The difference between property assignment and variable declaration — latter one sets DontDelete, whereas former one doesn't. That's why undeclared assignment creates a deletable property.
The difference between property assignment and variable declaration — latter one sets DontDelete, whereas former one doesn't. That's why undeclared assignment creates a deletable property.
回答by Pat
The main difference is when you're declaring variables inside a function. If you use var
when you're declaring a variable inside a function, then that variable becomes a local variable. However, if you don't use var
, then the variable becomes a global variable no matter where you declare it (inside or outside a function).
The main difference is when you're declaring variables inside a function. If you use var
when you're declaring a variable inside a function, then that variable becomes a local variable. However, if you don't use var
, then the variable becomes a global variable no matter where you declare it (inside or outside a function).
回答by Jay Bhatt
delete is only effective on an object's properties. It has no effect on variable or function names.
delete is only effective on an object's properties. It has no effect on variable or function names.
In your case x = 42; declares variable X and makes it the property of the Global object. So it returns true.
In your case x = 42; declares variable X and makes it the property of the Global object. So it returns true.
And var y = 43; declares a global variable which is not part of any object so it returns false.
And var y = 43; declares a global variable which is not part of any object so it returns false.