简体   繁体   English

IIFE内部的功能无法识别

[英]function inside of IIFE not recognized

I have an IIFE that I am trying to make into a bookmarklet. 我有一个IIFE,尝试将其制作为书签。 I would like to have the modal the bookmarklet will pop up have some buttons that will call a function. 我希望模态化的小书签会弹出,其中包含一些将调用函数的按钮。 However, when I have a structure like this: 但是,当我有这样的结构时:

(function(){

   var __myFn = function(str){ //also have tried function __myFn(){..}
      alert(str);
   }

   //some AJAX that builds HTML with the `result`s

   document.getElementById("resultDiv").innerHTML = "<span onclick='__myFn(" + result.someData+ ")'>test</span>


}());

I get Uncaught ReferenceError: __myFn is not defined 我收到Uncaught ReferenceError: __myFn is not defined

How can I make this function recognized? 如何使该功能得到认可? Is there another way? 还有另一种方法吗?

When you use the var keyword, you're creating a variable that is local to the scope of the enclosing context. 当使用var关键字时,您正在创建一个变量,该变量对于封闭上下文的作用域是局部的。 Since the enclosing context is a function, __myFn is local to the function itself and not known outside (ie, not known in the global context). 由于封闭上下文是一个函数,因此__myFn对于函数本身__myFn是本地的,并且在外部未知(即,在全局上下文中未知)。

If you want to use something that is inside, you would have to return a reference to it. 如果要使用其中的某些内容,则必须返回对其的引用。 You can use something like the module pattern for this: 您可以为此使用类似模块模式的内容:

var myModule = (function(){

   var __myFn = function(str) { 
      alert(str);
   }

   return {
       myFn: __myFn
   };

})();

Then you can do: 然后,您可以执行以下操作:

//some AJAX that builds HTML with the `result`s

document.getElementById("resultDiv").innerHTML = "<span onclick='myModule.myFn(" + result.someData+ ")'>test</span>

However, I recommend not binding your event handler this way. 但是,我建议不要以这种方式绑定事件处理程序。 Use jQuery or use DOM methods ( addEventListener ) to bind event handlers. 使用jQuery或使用DOM方法( addEventListener )绑定事件处理程序。 This way you could even do it inside the IIFE itself, which means you don't even have to return something from the IIFE. 这样,您甚至可以在IIFE内部进行操作,这意味着您甚至不必从IIFE返回任何内容。 This means your global context is not polluted. 这意味着您的全局环境没有受到污染。 Right now, the only reason you have to return something from the IIFE is because you are binding an event-handler inline via HTML. 现在,您必须从IIFE返回某些信息的唯一原因是因为您要通过HTML内联绑定事件处理程序。

Here are two examples. 这是两个例子。 The first one assumes that the IIFE returns a reference to __myFn : 第一种假定IIFE返回到参考__myFn

var resultDiv = document.getElementById("resultDiv");
resultDiv.addEventListener("click", myModule.myFn, false);

Here is the second example that does it within the IIFE itself: 这是在IIFE内部执行此操作的第二个示例:

(function(){

   var __myFn = function(str) { 
      alert(str);
   }

   var resultDiv = document.getElementById("resultDiv");
   resultDiv.addEventListener("click", __myFn, false);

})();

For reasons I don't understand, I have found that taking the "var" off the declaration makes it work. 由于我不明白的原因,我发现删除声明中的“ var”使其生效。

Example 1 (fails): 示例1(失败):

(function() {

    var counter = 0;
    var init = function() { console.log("init called"); }
})();

init(); // regardless of whether I named the function or not, this fails.

HOWEVER: Example 2 (works): 但是:示例2(有效):

(function() {
    var counter = 0;
    init = function() { console.log("init called"); }
})();

init(); // this calls the function just fine.

Note you can add any number of functions inside the iife this way just fine. 注意,您可以通过这种方式在iife中添加任意数量的函数。

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

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