这是双文件准备好了吗? [英] Is this a double document ready?

查看:64
本文介绍了这是双文件准备好了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个脚本,并找到以下内容,我真的找不到任何意义的信息

I'm working with a script and have found the following, which I really can't find any info of what it means

(function($) {
    $(document).ready(function(e) {


       ... bla bla bla ...

    });
}) (jQuery);

(function($){})(jQuery) code>与 $(function(){})相同; ?如果是这样,为什么有人定义两次 document.ready

Is (function($){}) (jQuery); the same as $(function () {}); ? and if so, why would somebody define twice document.ready ?

推荐答案

不,这不一样。这是一个匿名函数,它正在传递jQuery对象,以确保它可以作为函数范围内的局部变量 $ 使用,即使全局变量 $ 被另一个库覆盖。它完全不同于 $(function(){}) $(document).ready(function(){})

No, it's not the same. It's an anonymous function which is being passed the jQuery object, to insure that it is available as the local variable $ within the scope of the function, even if the global variable $ is overwritten by another library. It is completely different than $(function () { }) and $(document).ready(function () { }).

jQuery插件创作文档:


[创建插件时] 是最佳做法将jQuery传递给将其映射到美元符号的IIFE(即时调用的函数表达式),以便在其执行范围内不能被其他库覆盖。

[When authoring a plugin] it's a best practice to pass jQuery to an IIFE (Immediately Invoked Function Expression) that maps it to the dollar sign so it can't be overwritten by another library in the scope of its execution.

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

   // Do your awesome plugin stuff here

 };
})( jQuery );


这篇关于这是双文件准备好了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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