jQuery变量阴影

Mar*_*riy 2 javascript jquery

jQuery中有奇怪的模式:

var jQuery = (function() {

// Define a local copy of jQuery
var jQuery = function( selector, context ) {
 ...
return jQuery;

})();
Run Code Online (Sandbox Code Playgroud)

这有什么实际的原因?为什么不公开内部jQuery函数?它只适用于内部jQuery和外部jQuery中的名称冲突,因为它们都在闭包中.

lon*_*day 5

jQuery.noConflict(true)删除jQuery的全局名称.但是,如果不使用对象的某些名称来编程jQuery库的其余部分是不可能的,因此需要使用本地的非公开名称.为方便起见,它们重新定义jQuery为匿名函数范围内的变量.