English 中文(简体)
Is this scoping possible in javascript?
原标题:

I am working on a javascript framework. I have several independent scripts that look like this:

core.modules.example_module = function(sandbox){
    console.log( wot from constructor == , wot);

  return{
    init : function(){
      console.log( wot from init == , wot);
    }
  };
};

this function is called from another external script. I am trying to pass variables in to this function so that they can be accessed without using the this keyword.

The above example will error out saying wot is undefined.

If i wrap the function in an anonymous function and declare the variables there I get the expected desired results

(function(){

var wot =  omg ;

core.modules.example_module = function(sandbox){
    console.log( wot from creator == , wot);

  return{
    init : function(){
      console.log( wot from init == , wot);
    }
  };
};

})();

What i am trying to do is declare the variables further up the scope chain so they can be accessed in the module without using the this keyword like the second example. I don t believe this is possible as it looks like the functions execution scope is sealed on declaration of the function.

update
To clarify where I am trying to define wot. In a separate javascript file I have an object that calls a register module function like this

core = function(){
   var module_data = Array();
   return{
    registerModule(){
      var wot = "this is the wot value";
      module_data[ example_module ] = core.modules.example_module();
    }
  };
};
最佳回答

What you re looking for is called "dynamic scoping", where bindings are resolved by searching the current call-chain. It s not too common outside of the Lisp family (Perl supports it, via the local keyword). Dynamic scoping is not supported in JS, which uses lexical scoping.

问题回答

Consider this example, using your code

var core = {}; // define an object literal
core.modules = {}; // define modules property as an object

var wot=  Muhahaha! ;

core.modules.example_module = function(sandbox){

  console.log( wot from creator == , wot);

  return {
    init: function() {
       console.log( wot from init == , wot);

    }
  }
}

// logs wot from creator == Muhahaha! to the console    
var anObject = core.modules.example_module(); 

// logs wot from init == Muhahaha! to the console
anObject.init(); 

So long as wot is defined somewhere in the scope chain for core.modules.example_module at the point at which it is executed, this will work as expected.

Slightly off topic, but you ve touched upon the scope of functions. Functions have lexical scope, that is they create their scope at the point at which they are defined (as opposed to executed) and allows for closures to be created; A closure is created when a function has keeps a link to it s parent scope even after the parent has returned.

Putting var wot; at the beginning of your constructor ought to do it

core.modules.example_module = function(sandbox){
  var wot;
  wot =  foo ; //just so you can see it working
  console.log( wot from constructor == , wot);

  return{
    init : function(){
      console.log( wot from init == , wot);
    }
  };
};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签