jQuery插件语法包装器 [英] jquery plugin syntax wrapper

查看:53
本文介绍了jQuery插件语法包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这到底是做什么的?我知道它被jquery插件包住了,但是对它的功能并没有真正的了解.

What exactly does this do? I know that it gets wrapped around jquery plugins, but have no real understanding of what it does.

(function ($, undefined) {

// Plugin goes here

})(jQuery);

推荐答案

()()是一种在javascript中运行一段代码的方法.第一对括号是代码部分,第二对括号是执行顺序,您可以像调用函数一样传递变量.

()() is a way to run a piece of code in javascript. The first pair of parentesis is the code part and the second is for execute order and you can pass variables like calling a function.

在第一个括号内定义函数,该函数接受两个参数,并使用参数jQuery(它是jQuery的核心对象)执行该函数.为此,您要将jQuery对象作为变量传递给函数,并将其命名为$.因为您的函数是在该作用域中定义的,所以您的代码不会与全局作用域或包含您的代码的作用域上的另一个$变量冲突.

Within first parentesis you define your function, which takes two arguments and you execute it with a parameter jQuery which is core jQuery object. To do so, you are passing jQuery object to your function as a variable and name it $. Because your function is defined in that scope your code doesn't conflict with another $ variable on global scope or scope that contains your code.

如您所见,还有另一个变量:undefined,但没有参数.通过不传递第二个参数,第二个变量将是未定义的,并且由于它的名称是未定义的,因此您可以在代码中自由使用它.有关未定义的更多信息,您可以在此处

As you can see there is another variable: undefined but no parameter for it. By not passing a second parameter second variable will be undefined and because it's name is undefined you can use it in your code freely. For more info about undefined you can look here

我希望这足够清楚.

这篇关于jQuery插件语法包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆