如何将外部变量传递给私有 javascript 外部闭包函数?

发布于 2024-08-12 08:59:13 字数 502 浏览 4 评论 0原文

我可能在这方面做出了一些糟糕的设计选择。我有几个像这样实例化的对象。

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

我想这样做:

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

    var trial = core.modules.trial();

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

我试图将 omgpi 变量分配给外部函数的私有范围。通常,您会在返回任何内容之前在外部函数中执行 var omgpi 。但当调用此函数时,我尝试从外部脚本执行此操作

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

辞慾 2024-08-19 08:59:13

您可以猴子修补 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
};

请参阅 call 的文档

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.

嘿嘿嘿 2024-08-19 08:59:13

这是你想要的吗?

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

Is this what you want?

core.modules.trial = function(sandbox){
  var c = arguments.callee;
  return{
    alert_private : function(){
      alert(c.omgpi);
    }
  };
};
自演自醉 2024-08-19 08:59:13
core.modules.trial = function(sandbox){
  var self = {
    alert_private: function(){
      alert(self.omgpi);
    }
  };

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

  return self;
};
狼亦尘 2024-08-19 08:59:13

如果您需要 omgpi 位于闭包中,则需要从内部设置它。你不能在你不参与的闭包中设置东西。

core.modules.trial = function(sandbox){

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

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

但每当您调用 core.modules.Trial() 时,this 都会引用 modules,因为它就像父级。因此,您可以将值粘贴在模块中,如下所示:

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

然后剩下的就可以了:

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

顺便说一句,您的原始代码有一个错误:

core.modules.trial[omgpi]

它使用变量 omgpi 的作为键。您需要 core.modules.Trial.omgpicore.modules.Trial["omgpi"]

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文