微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

闭包 – 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?

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐