开发者

Javascript question: about variable definition

I have no idea why it didn't work if I specify a variable with 'var': like this:

var mytool = function(){
      return {
   开发者_开发知识库      method: function(){}
     }
}();

And later I use it in the same template: mytool.method. This will output mytool was not defined.

But if I define it like this:

     mytool = function(){
          return {
             method: function(){}
         }
    }();

Then it works.


Javascript has function scope. A variable is in scope within the function it was declared in, which also includes any functions you may define within that function.

function () {
    var x;

    function () {
        // x is in scope here
        x = 42;

        y = 'foo';
    }

    // x is in scope here
}

// x is out of scope here

// y is in scope here

When declaring a variable, you use the var keyword.
If you don't use the var keyword, Javascript will traverse up the scope chain, expecting to find the variable declared somewhere in a higher function. That's why the x = 42 assignment above assigns to the x that was declared with var x one level higher.

If you did not declare the variable at all before, Javascript will traverse all the way to the global object and make that variable there for you. The y variable above got attached to the global object as window.y and is therefore in scope outside the function is was declared in.
This is bad and you need to avoid it. Properly declare variables in the right scope, using var.


The code you have doesn't show enough to demonstrate the problem. var makes the variable being defined 'local' so it will only be available within the same function (javascript has function level scoping). Not using var makes it global, this is almost always not what you want. You might need to rearrange your code to fix the scope issues.


I take it that you're using this inside of some function:

function setup_mytool() {
    var mytool = function(){
          return {
             method: function(){}
         }
    }();
}

This creates the variable mytool in the function's scope; when the function setup_mytool exits, the variable is destroyed.

Saying window.mytool or window.my_global_collection.mytool will leave the variable mytool intact when the function exits.

Or:

var mytool;

function setup_mytool() {
    mytool = function(){
          return {
             method: function(){}
         }
    }();
}

will also do what I think it is you're intending.


The reason why you are getting variable undefined errors is because when you use var, the declared variable is scoped to the surrounding context, meaning that the variable's lifetime is limited to the lifetime of the surrounding context (function, block, whathaveyou).

If you don't use var however, you are effectively declaring a variable tied to global scope. (Usually a Very Bad Idea).

So, in your code, the reason why you are able to access the mytool variable somewhere else in your template is because you tied it to global scope, where in the case of using var, the variable went out of scope because it must have been declared within a function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜