问题
I am new to JavaScript.
<html>
<body>
<script type="text/javascript">
var x=5;
document.write(x);
document.write("<br />");
var x;
document.write(x);
</script>
</body>
</html>
Result is:
5
5
When x
is declared the second time it should be undefined, but it keeps the previous value. Please explain whether this redeclaration has any special purpose.
回答1:
You aren't really re-declaring the variable.
The variable statement in JavaScript, is subject to hoisting, that means that they are evaluated at parse-time and later in runtime the assignments are made.
Your code at the end of the parse phase, before the execution looks something like this:
var x;
x = 5;
document.write(x);
document.write("<br />");
document.write(x);
回答2:
var
alone does not perform an assignment. It only flags that when you use the variable name throughout the scope in which the var
occurs, you are talking about a local variable and not global (the controversial default). The var
is spotted when the function is parsed and holds throughout that scope, so where you put it is irrelevant:
var a= 0;
function foo() {
a= 1;
return a;
var a;
}
var b= foo();
alert('global a='+a+', local a='+b);
Results in global a= 0, local a= 1
: even though the var
statement is never reached in the course of execution of foo()
, it is still effective in making a
a local variable.
So declaring var x
a second time in the same scope is completely redundant. However you might sometimes still do it, typically when you re-use a local variable name for a second independent use within the same function. Most commonly:
for (var i= 0; i<onething.length; i++) {
...do some trivial loop...
}
for (var i= 0; i<anotherthing.length; i++) {
...do another trivial loop...
}
Whilst you could certainly omit the second var
, and tools like jslint
would demand you do so, it might not actually be a good idea.
Imagine you later change or remove the first loop so that it no longer declares i
to be var
. Now the remaining second loop suddenly changes meaning from a local to a global variable. If you fail to notice when updating the first loop that the second loop has a hidden dependency on it (and you might very well fail to notice that given how the eyes elide the pattern for(...=0 ; ...<...; ...++)
into “oh, that's just a standard iterator”), you've got a subtle and annoying-to-debug problem.
回答3:
so when the second time when its declared x should be undefined
What part of the specification says this?
"Undefined behaviour" does not mean "the variable will be undefined
".
回答4:
That second var x is totally superfluous.
回答5:
Within the same scope, it is totally unnecessary to "redeclare" a variable.
回答6:
As far as my understanding of javascript goes, the use of the var
keyword is completely optional in the global scope. It's a different story for functions.
When inside a function, use the var
keyword to indicate that the variable is local to the function (as opposed to being global by default).
I personally use var
in the global scope to show that a variable is being declared and/or utilized for the first time.
You can reference http://www.w3schools.com/js/js_variables.asp for more info.
回答7:
Also, a programmer might want to use var
to localize a variable:
<script>
var x= 'this is global x';
function my_x() {
var x= 'localized x';
alert(x);
}
my_x();
alert(x);
</script>
回答8:
You should never redeclare a variable within the same scope, if you really want to change this then assign to it. Redeclaration is not required to create a different object in this dynamic language, if you want x to be a string just assign:
x = "hello";
It is not required that you set it back to undefined or redeclare first.
Please note that changing the variable type is not very good practice in most situations, simply stating that it is a possibility if that's what you require.
回答9:
I recently wrote code like:
var obj1 = get_an_object();
var v = obj1.get_velocity();
v += changes_in_velocity();
obj1.set_velocity(v);
var obj2 = get_an_object();
var v = obj2.get_velocity();
v += changes_in_velocity();
obj2.set_velocity(v);
(The actual code was more complicated and less repetitive)
So far as the browser is concerned, the second var v
statement was redundant and pointless. To me, it served two purposes. It said to forget everything I knew about the old v
, because this was a new usage. And it meant I could re-order the code, or comment out the first half, without breaking things.
来源:https://stackoverflow.com/questions/1864013/is-there-any-purpose-to-redeclaring-javascript-variables