自执行函数 jquery vs javascript 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19491650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
self executing function jquery vs javascript difference
提问by Javascript Coder
What are the difference among -
有什么区别——
First :-
第一的 :-
(function () {
var Book = 'hello';
}());
Second:-
第二:-
(function () {
var Book = 'hello';
})();
First and second are similar some how in work..
第一个和第二个在工作中有些相似。
Third :-
第三 :-
(function ($) {
var Book = 'hello';
})(jQuery);
What pattern I need to use and where in my coding.. Third module pattern I have seen while I was reading a article related to backboneJS.
我需要使用什么模式以及在我的编码中的位置.. 我在阅读与backboneJS 相关的文章时看到的第三个模块模式。
What I understood from Third one "self executing function with the argument “jQuery”" ....
我从第三个“带有参数“jQuery”的自执行函数”中理解的......
Can any please give me some idea about Immediately Invoked Function Expressions (IIFE).
任何人都可以给我一些关于立即调用函数表达式(IIFE)的想法。
Thanks !!
谢谢 !!
采纳答案by Jorgeblom
In all cases you are doing an anonymous function. I think 1 is the same as 2. In the third case you are passing jQuery as an argument. This is done when you want to encapsulate jQuery within your function's scope.
在所有情况下,您都在执行匿名函数。我认为 1 与 2 相同。在第三种情况下,您将 jQuery 作为参数传递。当您想将 jQuery 封装在您的函数范围内时,就会完成此操作。
For instance, in your application, jQuery var could be jQuery. But within your anonymous function you may want to use it as $.
例如,在您的应用程序中,jQuery var 可以是 jQuery。但是在您的匿名函数中,您可能希望将其用作 $。
(function ($) {
//Here jQuery is $
var Book = $(document.body).text();
})(jQuery);
//Out of your function, you user jQuery as jQuery (in this example)
var Book = jQuery(document.body).text();
回答by Jai
This is called a closure to avoid conflicts with other libraries such as mootools
which are using $
. This way you can ensure to use $
in that function with passing jQuery
as a param.
这称为闭包,以避免与其他库(例如mootools
使用$
. 通过这种方式,您可以确保$
在该函数中使用并jQuery
作为参数传递。
(function ($) {
$(function () { // Here in this block you can use '$' in place of jQuery
.......
});
})(jQuery); //<----passing jquery to avoid any conflict with other libraries.
回答by Neil
Immediately-invoked function expressions (IIFE) is one of the core JavaScript features. Its main aim is not to clutter the namespaces with disposable functions and variables.
立即调用函数表达式 (IIFE) 是 JavaScript 的核心功能之一。它的主要目的不是用一次性函数和变量来弄乱命名空间。
if you use a variable or a function only once, you don't need to make it available for the rest of the code (therefore you make a private access, for example). In case of functions, you may just let them be anonymous, just like the following:
如果您只使用一次变量或函数,则不需要使其可用于其余代码(例如,因此您进行私有访问)。在函数的情况下,你可以让它们匿名,就像下面这样:
(function(){
console.log("Hello symfony world!");
}());
Furthermore here is a useful explanatory videoin less than 7 minutes
此外,这里有一个不到 7 分钟的有用解释视频
回答by Hugo Tunius
As the other answers pointed out they are all self executing anonymous function or immediate anonymous functions.
正如其他答案所指出的那样,它们都是自执行匿名函数或直接匿名函数。
The third example is use to create aliases for variables outside the function. This is a good way to prevent name conflicts and creating code where it's possible to easily change a module used in the function. It's essentially a form of dependency injection.
第三个例子用于为函数外的变量创建别名。这是防止名称冲突和创建可以轻松更改函数中使用的模块的代码的好方法。它本质上是依赖注入的一种形式。
(function (doc, win, $, myModule) {
// Code
}(document, window, jQuery, window.MYAPP.myModule));
doc
, win
, $
and myModule
are injected variables. With this pattern it's trivial to change any of the injected components. Like this
doc
, win
,$
和myModule
是注入变量。使用这种模式,改变任何注入的组件是微不足道的。像这样
(function (doc, win, $, myModule) {
// Code
}(document, window, jQuery, window.MYAPP.myModule2)); //Use myModule2 instead myModule
回答by MirroredFate
Like every other answer has said, in the third function you are passing JQuery
to the function.
就像其他所有答案所说的那样,在第三个函数中,您将传递JQuery
给该函数。
I would like to take a moment and explain why the first two are the same.
我想花点时间解释一下为什么前两个是相同的。
When you create a function, the name of that function is really a function pointer.For instance, in function foo(){}
, foo
is a pointer to the function that you just made (which explains stuff like this). You dereference that pointer (and thus execute the function) by adding parenthesis at the end of the function name: foo()
.
当您创建一个函数时,该函数的名称实际上是一个函数指针。例如,在function foo(){}
,foo
是一个指向你刚才的功能(这也解释了类似的东西这样)。取消引用该指针(并因此执行该功能)通过在函数名的末尾添加括号:foo()
。
So if we look at that code again, in number one, first you create the function:
因此,如果我们再次查看该代码,首先要创建函数:
function () {
var Book = 'hello';
}
And then you dereference it, effectively executing the function: ()
然后你取消引用它,有效地执行函数: ()
In the second example, you surround the entirety of the function creation in parenthesis:
在第二个示例中,您将整个函数创建括在括号中:
(function () {
var Book = 'hello';
})
This ensures that you perform the creation operation before your next command, which is to dereference the function again: ()
. The parenthesis in this case are not really necessary, as the function will be created before it is executed anyway.
这确保您在下一个命令之前执行创建操作,即再次取消引用该函数:()
。这种情况下的括号并不是真正必要的,因为无论如何都会在执行之前创建函数。
回答by Andy
All three examples are Immediately Invoked Function Expressions(IIFE).
所有三个示例都是立即调用函数表达式( IIFE)。
The only difference is that in the third example jQuery
is being passed in as a variable allowing you to use it within the IIFE using its dollar naming convention. e.g.
唯一的区别是在第三个示例jQuery
中作为变量传入,允许您使用其美元命名约定在 IIFE 中使用它。例如
(function ($) {
var Book = 'hello';
$('#bookelement').html(Book);
})(jQuery);
回答by Gurpreet Singh
These all are self executing functions. Now days also known as Immediately Invoked Function Expressions (IIFE).
这些都是自执行功能。现在也称为立即调用函数表达式 (IIFE)。
First two are exactly same with slightly different syntax and third is passing a parameter
as jQuery object.
前两个完全相同,语法略有不同,第三个是传递一个parameter
jQuery 对象。
回答by Jamie Hutber
In fact, all three are self executing function's and ti really depends on what you are needing to do.
事实上,这三个函数都是自执行函数,ti 实际上取决于您需要做什么。
The only difference between is between 3. 1 and 2 are the same.
之间的唯一区别是在 3. 1 和 2 之间是相同的。
The difference with 3 is that you are passing a reference to jquery as an argument. Now all functions inside this annoyomus function have access to jque
与 3 的不同之处在于您将引用 jquery 作为参数传递。现在这个 annoyomus 函数中的所有函数都可以访问 jque
回答by Deepak Dixit
All of these are example of self invoking function.
所有这些都是自调用函数的例子。
This will give you a clear view:-
这会给你一个清晰的观点:-
var my_func = function(){
var internal_var = "Hello";
return internal_var;
};
var my_func2 = function(name){
var internal_var = "Hello";
return internal_var;
};
var long_var_name = "I can be some object or number or object or array";
var result1 = (my_func());
var result2 = (my_func)();
var result3 = (my_func2)(long_var_name);
console.log(result1, result2, result3);
Using this example you can compare it with the First, Secondand Thirdmethod.
使用此示例,您可以将其与First、Second和Third方法进行比较。