简体   繁体   English

如何在变量的闭包内调用函数?

[英]How can I call a function within a closure within a variable?

I'm a newbie in java but the code is below the explanation This is code from a game but the problem is: Take the navigator function, I click the navigator in the game and some sort of (ajax?) goes on and it gets logged. 我是Java的新手,但代码在解释下方,这是游戏代码,但问题是:使用导航器功能,单击游戏中的导航器,然后继续执行某种(ajax?),已记录。 But when I use my FireBug console I cannot send a message with the functions above the return line. 但是,当我使用FireBug控制台时,无法使用返回行上方的功能发送消息。 Essentially.. The only functions that ThisFunction.* shows is the ones returned at the bottom. 本质上.. ThisFunction。*显示的唯一函数是在底部返回的函数。 How can I invoke the Navigator function? 如何调用导航器功能? I've tried: ThisFunction.a.navigator(args here); 我试过了: ThisFunction.a.navigator(args here);

, but it says a is undefined.. it doesn't show in the autocomplete list either. ,但是它说a是未定义的。它也不会显示在自动填充列表中。

** I removed the code because it is from a game. **我删除了代码,因为它来自游戏。 Thanks for the help! 谢谢您的帮助! ** **

You somewhat nailed it on its head with this bit: 您用此位将其钉在了头上:

The only functions that ThisFunction.* shows is the ones returned at the bottom ThisFunction。*显示的唯一函数是底部返回的函数

That is the expected and purposeful functioning of the language. 那是语言的预期和有目的的功能。

Short answer: You have to return out of the closure anything that you want to be externally accessible... That could be the a variable, or it could be an api that could itself access the a variable while keeping it private from the exterior. 简短的答案:您必须从闭包中返回任何您希望可以从外部访问的内容...可能是变量,也可能是可以访问变量同时保持其对外部私有的api。 This is called lexical scoping and it is your friend. 这称为词汇作用域,它是您的朋友。

Example time: 时间示例:

var ThisFunction = (function() {
  var a = { navigator: "woot" };
  var b = function() {
    return a;
  }
});
ThisFunction.a; //a is null/undefined on the returned
ThisFunction.b; //b is defined yay
var aOUTSIDE = ThisFunction.b();
aOUTSIDE.navigator; // "woot"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Closures

So the only things that can get at A are the things that were var'd up in the same "scope" as a. 因此,唯一可以得到A的东西就是与a在同一“范围”内变化的东西。 So either you return A out of the scope or you return something from inside the scope that provides an API to either get at A, or to execute some of A's internals... 因此,您要么在范围外返回A,要么从范围内返回某些东西,这些东西提供API以获得A或执行A的某些内部函数...

Depending on what OTHERFUNCTIONSHERE is, you can access it from within one of those functions, if they close over the variable a (why such cryptic var names by the way?). 根据OTHERFUNCTIONSHERE是什么,如果它们关闭了变量a (为什么这样的隐式var名称呢?),则可以从这些函数之一中访问它。 Otherwise, it's out of scope. 否则,超出范围。

Given that you're using Firebug, ThisFunction.%a.navigator(... args) should work ( .% is a Firebug-specific extension to the language). 假设您使用的是Firebug, ThisFunction.%a.navigator(... args)应该可以正常工作( .%是该语言的Firebug特定扩展名 )。 But as noted in other answers, it's impossible from pure JavaScript. 但是,正如其他答案所指出的那样,使用纯JavaScript是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM