闭包 – jQuery的noConflict:快速思考

前端之家收集整理的这篇文章主要介绍了闭包 – jQuery的noConflict:快速思考前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
文档说你可以像这样使用$.noConflict():
jQuery.noConflict();
(function($) { 
  $(function() {
    // more code using $as alias to jQuery
  });
})(jQuery);
// other code using $as an alias to the other library

它还声明调用它返回jQuery对象的实例,所以我可以这样做:

jQuery.noConflict()(function(){
    // code using jQuery
}); 
// other code using $as an alias to the other library

但是,这种组合有效吗?

(function($) { 
  $(function() {
    // more code using $as alias to jQuery
  });
})(jQuery.noConflict());
// other code using $as an alias to the other library

如果是这样,有没有理由不这样做?而且(如果它工作),为什么不总是使用这个方法来保证我们的闭包内部,$== jQuery?

解决方法@H_404_16@
最后一个方法也可以 – jQuery.noConflict()返回jQuery对象,该对象作为$参数传递给函数.

我没有理由不这样做,并且更喜欢其他方法.

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

猜你在找的jQuery相关文章