Firstly, a code block that looks like (function(){})()
is merely a function that is executed in place.
(首先,看起来像(function(){})()
的代码块仅仅是就地执行的功能。)
Let's break it down a little.(让我们分解一下。)
1. (
2. function(){}
3. )
4. ()
Line 2 is a plain function, wrapped in parenthesis to tell the runtime to return the function to the parent scope, once it's returned the function is executed using line 4, maybe reading through these steps will help
(第2行是一个普通函数,用括号括起来,告诉运行时将函数返回到父作用域,一旦返回,则使用第4行执行该函数,也许通读这些步骤会有所帮助)
1. function(){ .. }
2. (1)
3. 2()
You can see that 1 is the declaration, 2 is returning the function and 3 is just executing the function.
(您可以看到1是声明,2是返回函数,3只是执行函数。)
An example of how it would be used.
(有关如何使用它的示例。)
(function(doc){
doc.location = '/';
})(document);//This is passed into the function above
As for the other questions about the plugins:
(至于关于插件的其他问题:)
Type 1: This is not a actually a plugin, it's an object passed as a function, as plugins tend to be functions.
(类型1:这实际上不是插件,而是作为函数传递的对象,因为插件往往是函数。)
Type 2: This is again not a plugin as it does not extend the $.fn
object.
(类型2:再次不是插件,因为它没有扩展$.fn
对象。)
It's just an extenstion of the jQuery core, although the outcome is the same.(尽管结果是相同的,但这只是jQuery核心的扩展。)
This is if you want to add traversing functions such as toArray and so on.(这是如果要添加遍历函数,例如toArray等。)
Type 3: This is the best method to add a plugin, the extended prototype of jQuery takes an object holding your plugin name and function and adds it to the plugin library for you.
(类型3:这是添加插件的最佳方法,jQuery的扩展原型采用一个对象来保存您的插件名称和功能,并将其添加到您的插件库中。)