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 on e 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
Add a ment  | 

6 Answers 6

Reset to default 9

Because 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 on e 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
Add a ment  | 

6 Answers 6

Reset to default 9

Because 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