简体   繁体   English

为什么 jQuery 在检查与全局对象的冲突时使用“深度”?

[英]Why does jQuery use 'deep' while checking conflicts with the global object?

I was observing the source code of the jQuery library, and I noticed one thing.我在观察 jQuery 库的源代码时,我注意到了一件事。 At the end of the whole library, a method checks if there is any other function called jQuery or $ on the global object.在整个库的末尾,一个方法会检查全局对象上是否还有其他名为jQuery$的函数。

Anyway, here is that method:无论如何,这是该方法:

var

    // Map over jQuery in case of overwrite
    _jQuery = window.jQuery,

    // Map over the $ in case of overwrite
    _$ = window.$;

jQuery.noConflict = function( deep ) {
    if ( window.$ === jQuery ) {
        window.$ = _$;
    }

    if ( deep && window.jQuery === jQuery ) {
        window.jQuery = _jQuery;
    }

    return jQuery;
};

I couldn't understand what deep actually does here.我无法理解deep在这里实际上做了什么。 How having a deep copy situation can change the result of that function?深拷贝情况如何改变该函数的结果?

According to jQuery's documentation , the function jQuery.noConflict() restores the global variable window.$ to its previous value, from before jQuery was initialized:根据 jQuery 的文档,函数jQuery.noConflict()将全局变量window.$恢复到它之前的值,从 jQuery 初始化之前开始:

Many JavaScript libraries use $ as a function or variable name, just as jQuery does.许多 JavaScript 库使用$作为函数或变量名,就像 jQuery 一样。 In jQuery's case, $ is just an alias for jQuery , so all functionality is available without using $ .在 jQuery 的例子中, $只是jQuery的别名,所以所有功能都可以在不使用$的情况下使用。 If you need to use another JavaScript library alongside jQuery, return control of $ back to the other library with a call to $.noConflict() .如果您需要在 jQuery 旁边使用另一个 JavaScript 库,请通过调用$.noConflict()将 $ 的控制权返回给另一个库。 Old references of $ are saved during jQuery initialization; $的旧引用在 jQuery 初始化期间被保存; noConflict() simply restores them. noConflict()只是恢复它们。

When initialized, jQuery overwrites the global variable $ .初始化时,jQuery 会覆盖全局变量$ If you are using another library that writes to window.$ , jQuery might overwrite window.$ , causing the other library to break.如果您正在使用另一个写入window.$的库,jQuery 可能会覆盖window.$ ,从而导致另一个库中断。

In such cases, you can call jQuery.noConflict() to have jQuery restore window.$ to its previous value.在这种情况下,您可以调用jQuery.noConflict()让 jQuery 将window.$恢复为之前的值。 Since jQuery is also available through window.jQuery , you can still use jQuery after calling noConfilct() (for example by writing window.jQuery("#id") ).由于 jQuery 也可以通过window.jQuery使用,所以在调用noConfilct()之后仍然可以使用 jQuery(例如通过编写window.jQuery("#id") )。

But what if you are running two versions of jQuery on the same page?但是如果你在同一个页面上运行两个版本的 jQuery 呢? In this case, the second version will overwrite both window.$ and window.jQuery , which means you won't have access to the first version of jQuery after the second version initializes.在这种情况下,第二个版本将覆盖window.$window.jQuery ,这意味着在第二个版本初始化后您将无法访问第一个版本的 jQuery。

This is where the deep parameter comes into play.这就是deep参数发挥作用的地方。 This parameter isn't related to "deep copy", it's more like a "deep clean".此参数与“深度复制”无关,它更像是“深度清理”。 calling jQuery.noConflict(true) restores both window.$ and window.jQuery .调用jQuery.noConflict(true)同时恢复window.$window.jQuery If you call this function from the second version you loaded, it will restore the first version into the global scope.如果您从加载的第二个版本调用此函数,它会将第一个版本恢复到全局范围内。

Now let's take another look at the code you mentioned:现在让我们再看一下您提到的代码:

var
    _jQuery = window.jQuery, // Store the previous value of `window.jQuery`
    _$ = window.$; // Store the previous value of `window.$`

jQuery.noConflict = function( deep ) {
    if ( window.$ === jQuery ) {
        window.$ = _$; // Restore `window.$` to the value we previously stored in the variable
    }

    if ( deep && window.jQuery === jQuery ) {
        window.jQuery = _jQuery; // If called with the parameter `deep`, also restore `window.jQuery`
    }

    return jQuery; // Return a reference to this version of jQuery
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM