什么(function($){})(jQuery);意思? [英] What does (function($) {})(jQuery); mean?

查看:135
本文介绍了什么(function($){})(jQuery);意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始编写jQuery插件。我写了三个小插件,但我只是简单地将行复制到我的所有插件中而实际上并不知道它意味着什么。有人能告诉我更多关于这些的事吗?也许有一天解释会在编写框架时派上用场:)

I am just starting out with writing jQuery plugins. I wrote three small plugins but I have been simply copying the line into all my plugins without actually knowing what it means. Can someone tell me a little more about these? Perhaps an explanation will come in handy someday when writing a framework :)

这是做什么用的? (我知道它以某种方式扩展了jQuery,但还有其他有趣的事情要知道)

What does this do? (I know it extends jQuery somehow but is there anything else interesting to know about this)

(function($) {

})(jQuery);

以下两种编写插件的方式有什么区别:

What is the difference between the following two ways of writing a plugin:

类型1:

(function($) {
    $.fn.jPluginName = {

        },

        $.fn.jPluginName.defaults = {

        }
})(jQuery);

类型2:

(function($) {
    $.jPluginName = {

        }
})(jQuery);

类型3:

(function($){

    //Attach this new method to jQuery
    $.fn.extend({ 

        var defaults = {  
        }  

        var options =  $.extend(defaults, options);  

        //This is where you write your plugin's name
        pluginname: function() {

            //Iterate over the current set of matched elements
            return this.each(function() {

                //code to be inserted here

            });
        }
    }); 
})(jQuery);

我可能会离开这里,也许意味着同样的事情。我很迷惑。在某些情况下,这个似乎没有在我使用Type 1编写的插件中工作。到目前为止,类型3似乎对我来说最优雅,但我想知道其他人也是。

I could be way off here and maybe all mean the same thing. I am confused. In some cases, this doesn't seem to be working in a plugin that I was writing using Type 1. So far, Type 3 seems the most elegant to me but I'd like to know about the others as well.

推荐答案

首先,代码块看起来像(function(){}) ()只是一个在适当位置执行的函数。让我们分解一下。

Firstly, a code block that looks like (function(){})() is merely a function that is executed in place. Let's break it down a little.

1. (
2.    function(){}
3. )
4. ()

第2行是一个普通函数,用括号括起来告诉运行时将函数返回到父作用域,一旦返回该函数就使用第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

1. function(){ .. }
2. (1)
3. 2()

你可以看到1是声明,2是返回函数,3只是执行函数。

You can see that 1 is the declaration, 2 is returning the function and 3 is just executing the function.

如何使用它的一个例子。

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:

类型1:这实际上不是一个插件,它是一个作为函数传递的对象,因为插件往往是函数。

Type 1: This is not a actually a plugin, it's an object passed as a function, as plugins tend to be functions.

类型2:这也不是插件,因为它不会扩展 $。fn 对象。它只是jQuery核心的扩展,尽管结果是一样的。这是如果你想添加遍历函数,如toArray等。

Type 2: This is again not a plugin as it does not extend the $.fn object. It's just an extenstion of the jQuery core, although the outcome is the same. This is if you want to add traversing functions such as toArray and so on.

类型3:这是添加插件的最佳方法,jQuery的扩展原型需要一个包含插件名称和功能的对象,并将其添加到插件库中。

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.

这篇关于什么(function($){})(jQuery);意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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