开发者

fake function local vars

Is there any way to introduce the local variable "iWantThisHere" here, so i can call obj.n()

var obj = { 
    test: "sure", 
    n : function(){ console.log(iWantThisHere); }
};

i tried with:

function fake(methods){
    var F =  new Function('methods', "var iWantThisHere = 'u have it', obj = {}; for( var meth in methods){ obj[meth] = methods[meth] } return obj;");

    return F(methods); }

console.log(fake(obj), fake(开发者_如何学Cobj).n());

i dont want to use globals here. Imagine a modul system where each modul uses for example a util class, which is defined for example in a App object. So i always have to type for eg App.utils.map or something like that. It would be nice to have a shortcut to this util function which is a local variable in the modul context itself.

EDIT --- theres no way to do that ----


Introduce a closure:

var obj = (function(){
    var R = {};

    R.iWantThisHere = 5;

    R.log = function() {
        console.log(R.iWantThisHere);
    }

    return R;
})();

Demo:

> obj.log()
5
> obj.iWantThisHere = 6
> obj.log()
6

This is the most common (dare I say recommended) way to do javascript programming, by creating functions to restrict variables to a scope. It is necessary because javascript does not have block-level scoping.


If your question is, "How can I stuff a new named value into an alreay-extant scope?", the answer is that you cannot. The closest you can come is:

var obj = (function() {
  var iWantThisHere = "whatever";
  return {
    test: "sure",
    n: function() { var x = iWantThisHere + "yaay I have my special variable"; }
  };
})();

That gives you the "obj" that you have in your original example, plus for any functions defined in that object, you've got access to a "relatively global" variable they all share.


Any particular reason to do this the hard way? Why not just add an argument to the function in obj.n?

var obj = { 
  test: "sure", 
  n : function(iWantThisHere){ console.log(iWantThisHere); }
};

obj.n("yeah");

Or even simpler (although scoping gets uglier):

var iWantThisHere = "dance time";
var obj = {
  test: "sure", 
  n : function(){ console.log(iWantThisHere); }
};
obj.n();

Or with a closure, plus some details on how you separate their public and private parts:

var obj = (function() {
  var privateVariable = "i'm private";

  function privilegedMethod() {
  };

  return {
    iWantThisHere: "",
    test: "sure",
    n: function() {
      console.log(this.iWantThisHere);
    },
    publicVariable: "i'm public",
    publicMethod: function() {}
  };
})();

obj.iWantThisHere = "hi";
obj.n(); // logs "hi"
obj.addedMethod = function() { console.log("I've been added"); }
obj.addedMethod(); // logs "I've been added";
console.log(obj.privateVariable); // logs undefined (unavailable outside closure)
console.log(obj.publicVariable); // logs "i'm public"

The other answers cover closure notation pretty well, too. But the bottom line is that closures and restricted scopes would be pointless in JavaScript if you could just go around injecting new variables everywhere.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜