jQuery的noConflict:快速思考

Jes*_*ing 8 jquery closures

文档说你可以像这样使用$ .noConflict():

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

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

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

但是,这种组合有效吗?

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

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

Ott*_*ger 5

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

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


Art*_*cto 5

我不明白为什么不能因为jQuery.noConflict 返回jQuery对象,被称为函数之前它的评估.