English 中文(简体)
How to pass external variables to a private javascript outer closure function?
原标题:

I may have made some poor design choices on this one. I have several objects being instanced like this.

core.modules.trial = function(sandbox){
  return{
    alert_private : function(){
      alert(omgpi);
    }
  };
};

I would like to do this:

   core.modules.trial[omgpi] = "external private var";

    var trial = core.modules.trial();

    trial.alert_private(); //would hopefully output "external private var"

I am trying to assign the omgpi variable to the private scope of the outer function. Normally you would do var omgpi within the outer function before returning anything. But I am trying to do this from an external script when this function is called

问题回答

You can monkey-patch core.modules.trial:

var old_constructor = core.modules.trial;
core.modules.trial = function(sandbox) {
  this.omgpi =  whatever ; // or an object you can add to as desired
  return old_constructor.call(this, sandbox); // rebinds to this this
};

See the documentation for call.

Is this what you want?

core.modules.trial = function(sandbox){
  var c = arguments.callee;
  return{
    alert_private : function(){
      alert(c.omgpi);
    }
  };
};
core.modules.trial = function(sandbox){
  var self = {
    alert_private: function(){
      alert(self.omgpi);
    }
  };

  return self;
};

If you need omgpi to be in the closure, you need to set it from within. You can t set things in closures you re not a part of.

core.modules.trial = function(sandbox){

    ///////////////////////////
    var omgpi = this.omgpi;
    ///////////////////////////

    return{
        alert_private : function(){
            alert(omgpi);
        }
    };
};

But whenever you call core.modules.trial(), this refers to modules because that s like the parent. So you could stick the value in modules like this:

core.modules.omgpi = "external private var";

Then the rest works:

var trial = core.modules.trial();
trial.alert_private(); // alerts "external private var"

By the way, your original code had a bug:

core.modules.trial[omgpi]

This uses the value of the variable omgpi as the key. You want either core.modules.trial.omgpi or core.modules.trial["omgpi"].





相关问题
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.

热门标签