views:

150

answers:

4

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

A: 

Is this what you want?

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

  return self;
};
Bill Zeller
+1  A: 

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.

Jeff Ober
this does not work as omgpi is called as an external symbol. have you tried it ?
gpilotino
This is very close to what I am trying to accomplish but wouldn't I have to prepend everything with this in the public methods? Also gpilontino is correct this doesn't work.
kevzettler
A: 

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"].

darkporter
thx darkporter, I have a lot of these modules similair to .trial that I am trying to avoid duplicating code in. Is there any way I can automate this var assignment in the .modules object or the core? so that the individual modules inherit it?
kevzettler
I suspect this code could be _way_ simplified. In your example will `trial` be called multiple times with different `omgpi` values? I.e. will you be doing this: core.modules.omgpi = "external private var"; var trial = core.modules.trial(); ... later ... core.modules.omgpi = "new value"; var otherTrial = core.modules.trial();
darkporter