为什么要写这样的代码Jquery

前端之家收集整理的这篇文章主要介绍了为什么要写这样的代码Jquery前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么要写这样的代码 Jquery
(function ($) {
    $(function () {
     .......
    });
})(jQuery);
@H_403_5@解决方法
这被称为关闭以避免与使用$的其他库冲突.这样,您可以确保在该函数中使用$,将jQuery作为参数传递.
(function ($) {
   $(function () {
    .......
   });
})(jQuery); //<----passing jquery to avoid any conflict with other libraries.

从文档:

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.

This is generally used to author plugins. Read out more here

原文链接:https://www.f2er.com/jquery/176022.html

猜你在找的jQuery相关文章