admin管理员组文章数量:1024363
I thought any variable defined in a function would be local but I can easily access variable 'e' outside of its function.
function change() {
var d = 6;
e = 7;
}
change();
alert(e); //> alerts 7
I thought any variable defined in a function would be local but I can easily access variable 'e' outside of its function.
function change() {
var d = 6;
e = 7;
}
change();
alert(e); //> alerts 7
Share
Improve this question
edited Apr 11, 2011 at 18:43
user142162
asked Apr 11, 2011 at 18:40
David GDavid G
97.1k41 gold badges172 silver badges258 bronze badges
1
-
2
Did you intend to put a ma after
var d = 6
? Reason why I ask is because of the extra indentation one
which is typical when defining multiple variables (on multiple lines) in a single var statement. If you replaced the semicolon with a ma,e
would be a local variable. – Cristian Sanchez Commented Apr 11, 2011 at 18:48
6 Answers
Reset to default 9Because new variables will enter the global scope by default. var
prevents this from happening by constraining a variable's existence to be within the current scope.
Because it was declared without var
it bees part of the global window
object.
You've not explicitly declared it as such, so it has taken global scope.
Thats because e is global by default, using var make a scope varible. You can read more about this in Javascript Garden Scope and Namespaces
I am guessing that you are going under this assumption that
JSLint expects that a var will be declared only once, and that it will be declared before it is used.
Problem with your code is you are using one var, but your second line has no var in front of it. That is pushing that varaible e into the global namespace.
Why is it happening? You used a semicolon instead of a ma in the variable declaration.
function change() {
var d = 6, //Change this to a ma
e = 7;
}
change();
alert(e); //will produce an error now
It is surprisingly easy to create global variables, here are some other gotchas I've seen.
// :-( antipattern: implied global variable
function sum(x, y) {
result = x + y; // result is global
return result;
}
// :-) better
function sum(x, y) {
var result = x + y; // result is local
return result;
}
// :-( antipattern: chain assignments as part of a var declaration
function foo() {
var a = b = 0; // b is global
}
// :-) better
function foo() {
var a, b;
a = b = 0; // both local
}
I thought any variable defined in a function would be local but I can easily access variable 'e' outside of its function.
function change() {
var d = 6;
e = 7;
}
change();
alert(e); //> alerts 7
I thought any variable defined in a function would be local but I can easily access variable 'e' outside of its function.
function change() {
var d = 6;
e = 7;
}
change();
alert(e); //> alerts 7
Share
Improve this question
edited Apr 11, 2011 at 18:43
user142162
asked Apr 11, 2011 at 18:40
David GDavid G
97.1k41 gold badges172 silver badges258 bronze badges
1
-
2
Did you intend to put a ma after
var d = 6
? Reason why I ask is because of the extra indentation one
which is typical when defining multiple variables (on multiple lines) in a single var statement. If you replaced the semicolon with a ma,e
would be a local variable. – Cristian Sanchez Commented Apr 11, 2011 at 18:48
6 Answers
Reset to default 9Because new variables will enter the global scope by default. var
prevents this from happening by constraining a variable's existence to be within the current scope.
Because it was declared without var
it bees part of the global window
object.
You've not explicitly declared it as such, so it has taken global scope.
Thats because e is global by default, using var make a scope varible. You can read more about this in Javascript Garden Scope and Namespaces
I am guessing that you are going under this assumption that
JSLint expects that a var will be declared only once, and that it will be declared before it is used.
Problem with your code is you are using one var, but your second line has no var in front of it. That is pushing that varaible e into the global namespace.
Why is it happening? You used a semicolon instead of a ma in the variable declaration.
function change() {
var d = 6, //Change this to a ma
e = 7;
}
change();
alert(e); //will produce an error now
It is surprisingly easy to create global variables, here are some other gotchas I've seen.
// :-( antipattern: implied global variable
function sum(x, y) {
result = x + y; // result is global
return result;
}
// :-) better
function sum(x, y) {
var result = x + y; // result is local
return result;
}
// :-( antipattern: chain assignments as part of a var declaration
function foo() {
var a = b = 0; // b is global
}
// :-) better
function foo() {
var a, b;
a = b = 0; // both local
}
本文标签: javascriptWhy is the variable inside this function globalStack Overflow
版权声明:本文标题:javascript - Why is the variable inside this function global? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745585006a2157536.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论