简体   繁体   English

调用IIFE内部的功能

[英]Invoking a function that is inside an IIFE

I am successfully being able to call a function ( function1 ) which exists outside of an IIFE. 我可以成功调用IIFE外部存在的函数( function1 )。

My question is how can I invoke function2 ? 我的问题是如何调用function2 Right now it returns function2 is undefined. 现在,它返回function2是不确定的。 I am thus sure that this is not the right way to call a function from an IIFE. 因此,我确信这不是从IIFE调用函数的正确方法。 So I would love to know the correct way and appreciate a brief explanation. 因此,我很想知道正确的方法,并希望获得简短的解释。

 function function1() { alert('1'); } (function($){ function function2(){ alert('2'); } })(jQuery); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" onkeypress="function1();"/> <input type="text" onkeypress="function2();"/> 

The onkeypress attribute runs in the global context, so it can't see the function inside your IIFE. onkeypress属性在全局上下文中运行,因此无法在IIFE中看到该函数。

Using attributes for event listeners like this is not a recommended practice. 建议不要像这样对事件侦听器使用属性。

Instead of doing it that way, it's better to attach the event listener from code inside your IIFE, perhaps like this: 与其那样做,不如从IIFE中的代码附加事件侦听器,也许像这样:

 (function($){ function function2(){ alert('2'); } $('#test2').on( 'keypress', function2 ); })(jQuery); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="test2" /> 

I added an id attribute to the input element, but you could also use a class or other ways to identify it when you add the event listener. 我向输入元素添加了id属性,但是在添加事件侦听器时,您也可以使用class或其他方式来标识它。

Your function is out of scope in the code that you have provided. 您提供的代码超出了您的功能范围。 You could either move function two out of the IIFE or register your event on the inputs inside the IIFE. 您可以将功能二移出IIFE,也可以将事件注册到IIFE的输入中。

 function one() { alert(1) } (function($) { function two() { alert(2) } $('.one').keypress(one) $('.two').keypress(two) })(jQuery) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="one" type="text" /> <input class="two" type="text" /> 

Use jQuery, of course! 当然使用jQuery!

 function function1() { console.log('1'); } (function($){ $('input').eq(1).keypress(function function2() { console.log('2'); }) })(jQuery); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" onkeypress="function1();"/> <input type="text"/> 

Yes I'm aware I completely side-stepped the issue. 是的,我知道我完全回避了这个问题。

The point is, you should be doing what is considered good practice, and not trying to force something which is considered bad practice into working. 关键是,您应该做被认为是好的实践,而不是试图强迫被认为是坏实践的事情起作用。

You can use it if you make function2 public 如果将function2公开,则可以使用它

 function function1() { alert('1'); } var myFuncs = (function($){ function function2(){ alert('2'); } return { fun2:function2 } })(jQuery); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" onkeypress="function1();"/> <input type="text" onkeypress="myFuncs.fun2();"/> 

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

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