深入學習jquery源碼之noConflict()
jQuery.noConflict([extreme])
概述:
運作這個函數将變量$的控制權讓渡給第一個實作它的那個庫。
執行
var jq=$.noConflict();
後,$ 将不再控制目前的jQuery, 而是讓渡給了jq變量,此時
jq("div")
和
jQuery("div")
是等價的。
這有助于確定jQuery不會與其他庫的$對象發生沖突。 在運作這個函數後,就隻能使用jQuery變量通路jQuery對象。例如,在要用到$("div p")的地方,就必須換成jQuery("div p")。 '''注意:'''這個函數必須在你導入jQuery檔案之後,并且在導入另一個導緻沖突的庫'''之前'''使用。當然也應當在其他沖突的庫被使用之前,除非jQuery是最後一個導入的。
參數:
extreme Boolean
傳入 true 來允許徹底将jQuery變量還原
使用:
将$引用的對象映射回原始的對象。
jQuery.noConflict();
// 使用 jQuery
jQuery("div p").hide();
// 使用其他庫的 $()
$("content").style.display = 'none';
恢複使用别名$,然後建立并執行一個函數,在這個函數的作用域中仍然将$作為jQuery的别名來使用。在這個函數中,原來的$對象是無效的。這個函數對于大多數不依賴于其他庫的插件都十分有效。
jQuery.noConflict();
(function($) {
$(function() {
// 使用 $ 作為 jQuery 别名的代碼
});
})(jQuery);
// 其他用 $ 作為别名的庫的代碼
建立一個新的别名用以在接下來的庫中使用jQuery對象。
var j = jQuery.noConflict();
// 基于 jQuery 的代碼
j("div p").hide();
// 基于其他庫的 $() 代碼
$("content").style.display = 'none';
完全将 jQuery 移到一個新的命名空間。
var dom = {};
dom.query = jQuery.noConflict(true);
// 新 jQuery 的代碼
dom.query("div p").hide();
// 另一個庫 $() 的代碼
$("content").style.display = 'none';
// 另一個版本 jQuery 的代碼
jQuery("div > p").hide();
jquery源碼
(function (global, factory) {
if (typeof module === "object" && typeof module.exports === "object") {
// For CommonJS and CommonJS-like environments where a proper window is present,
// execute the factory and get jQuery
// For environments that do not inherently posses a window with a document
// (such as Node.js), expose a jQuery-making factory as module.exports
// This accentuates the need for the creation of a real window
// e.g. var jQuery = require("jquery")(window);
// See ticket #14549 for more info
module.exports = global.document ?
factory(global, true) :
function (w) {
if (!w.document) {
throw new Error("jQuery requires a window with a document");
}
return factory(w);
};
} else {
factory(global);
}
// Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function (window, noGlobal) {
var deletedIds = [];
var slice = deletedIds.slice;
var concat = deletedIds.concat;
var push = deletedIds.push;
var indexOf = deletedIds.indexOf;
var class2type = {};
var toString = class2type.toString;
var hasOwn = class2type.hasOwnProperty;
var support = {};
var
version = "1.11.3",
// Define a local copy of jQuery
jQuery = function (selector, context) {
// The jQuery object is actually just the init constructor 'enhanced'
// Need init if jQuery is called (just allow error to be thrown if not included)
return new jQuery.fn.init(selector, context);
}
jQuery.fn = jQuery.prototype = {
// The current version of jQuery being used
jquery: version,
constructor: jQuery,
// Start with an empty selector
selector: "",
// The default length of a jQuery object is 0
length: 0,
toArray: function () {
return slice.call(this);
},
// Get the Nth element in the matched element set OR
// Get the whole matched element set as a clean array
get: function (num) {
return num != null ?
// Return just the one element from the set
(num < 0 ? this[num + this.length] : this[num]) :
// Return all the elements in a clean array
slice.call(this);
}
}
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;
};
// Expose jQuery and $ identifiers, even in
// AMD (#7102#comment:10, https://github.com/jquery/jquery/pull/557)
// and CommonJS for browser emulators (#13566)
if (typeof noGlobal === strundefined) {
window.jQuery = window.$ = jQuery;
}
return jQuery;
}));