立即调用函数表达式(IIFE)对正常函数的好处 [英] Benefit of Immediately-invoked function expression (IIFE) over a normal function
问题描述
我是javascript的新手,我读到模块模式提供某种命名空间并拥有私有和公共成员,例如:
I'm pretty new to javascript and I read about the module pattern to provide some sort of namespace and have both private and public members, for example:
var module = (function() {
var s = "Hello, i'm private and in closure!";
return {
myString : s,
myFunc: function() { alert(s); }
};
})();
我做看到它的好处,因为它给你一些面向对象编程的优点。但是我已经看到很多没有分配给变量的IIFE的例子。与你调用的普通函数相比,这(就我所见)没有任何优势:
I do see the benefits of that, because it gives you some of the advantages of object-oriented programming. But I've seen a lot of examples of an IIFE that doesn't get assigned to a variable. This (as far as I see) has no advantages at all compared to a normal function that you invoke:
(function() {
var s = "Hello I'm private!";
$('#myButton').on('click', function() {
alert(s);
});
})();
2。正常功能
2. Normal function
function Initialize() {
var s = "Hello I'm private!";
$('#myButton').on('click', function() {
alert(s);
});
}
Initialize();
它们都有私有变量,可以避免创建全局变量,并且它们都可以在不返回任何值的情况下执行变量。
虽然第二个让你可以选择一个比没有名字的潜在大型IIFE更多的好名字,让读者知道发生了什么。 答案我看到无处不在 '以避免命名空间污染'但两种方法都是这样做的,第一种方法有点难以理解?
They both have private variables that avoid the need of creating global variables and they both execute without returning any value to a variable. Although the second one gives you the option of choosing a good name that says a lot more than a potential large IIFE without the name, leaving the reader to find out what's happening. The answer I see everywhere is 'to avoid namespace pollution' but both approaches do that, the first one is just a bit harder to understand?
简而言之:
使用IIFE比我缺少的正常功能有什么好处?我为什么要使用它们?
推荐答案
有时你需要同时定义和调用函数而且只需要一次所以在这种情况下,匿名功能可以帮助您。在这种情况下,为函数命名然后调用它们只是过剩。
Sometimes you need to define and call function at the same time and only once so in this case anonymous function helps you. In such situations giving functions a name and then calling them is just excess.
有时你想创建一个命名空间
为你的变量。所以匿名
函数也可以帮助你。例如
Further sometimes you wants to create a namespace
for your variables. So anonymous
functions helps you there too. For example
(function($) {
$.fn.pluginName = function(opt) {
// implementation goes here...
}
}(jQuery));
在上述情况下,您可以安全地使用 $
as jQuery
代码中的同义词。
In above case you can safely use $
as jQuery
synonym in your code.
如果您定义一个名称如下所示的函数,那么它将创建带有你定义的函数名的全局变量。
If you define a function with name as shown below, then it will create global variable with function name as you defined.
function myFunction() {
// function code goes here.
}
myFunction();
但是如果你定义它没有名字那么它就不会创建任何全局变量而你的全局命名空间将会不被污染。
But if you define it without name then it won't create any global variable and your global namespace will not be polluted.
(function myFunction() {
// function code goes here.
}());
只有当您需要从代码中的不同位置调用时,带名称的函数才有用。
Function with names are useful only when you need to call them from different places in your code.
这篇关于立即调用函数表达式(IIFE)对正常函数的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!