在Javascript中,此语法是什么意思? [英] In Javascript, what does this syntax mean?
问题描述
当我看着jQuery插件时发现了这个片段,想知道它的实际作用
I found this snippet when i was looking at jQuery plugins and wonder what it actually does
一个jQuery插件框架:
A jQuery plugin skeleton:
(function($) {
...
})(jQuery);
推荐答案
这将创建一个匿名函数并直接调用它: 等同于
This creates a anonymous function and calls it directly: this is equivalent to
var fun = function(){};
fun();
它在jquery插件中用于确保与定义全局变量'$'的其他库的兼容性.在您的插件框架中,您将插件包装在一个匿名函数中,该函数接收一个名为"$"的参数(因此将覆盖全局变量"$"),然后将该匿名函数以"jQuery"作为参数进行调用,因此有效地$ = jQuery,但仅限于该匿名函数内.
its used in jquery plugins to ensure compatibility with other libraries defining a global variable '$'. in your plugin sekeleton, you wrap your plugin in a anonymous function, which receives an argument named '$' (thus overriding a global variable '$'), this anonymous function is then called with 'jQuery' as parameter, so effectively $ becomes = jQuery, but only within that anonymous function.
这篇关于在Javascript中,此语法是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!