近期在讀jQuery的源碼。
發現這里有個東西很難理解。
這里的 jQuery , jQuery.fn , jQuery,fn,init ,jQuery,prototype 都代表什么。
來看下jQuery的源碼是怎么樣定義的:
復制代碼 代碼如下:
(function( window, undefined ) {
var jQuery = (function() {
// 構建jQuery對象
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
// jQuery對象原型
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
// something to do
}
};
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
// 合并內容到第一個參數中,后續大部分功能都通過該函數擴展
// 通過jQuery.fn.extend擴展的函數,大部分都會調用通過jQuery.extend擴展的同名函數
jQuery.extend = jQuery.fn.extend = function() {};
// 在jQuery上擴展靜態方法
jQuery.extend({
// something to do
});
// 到這里,jQuery對象構造完成,后邊的代碼都是對jQuery或jQuery對象的擴展
return jQuery;
})();
window.jQuery = window.$ = jQuery;
})(window);
復制代碼 代碼如下:
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
復制代碼 代碼如下:
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
// something to do
}
};
復制代碼 代碼如下:
jQuery.fn = jQuery.prototype
復制代碼 代碼如下:
jQuery.fn.init.prototype = jQuery.fn;
復制代碼 代碼如下:
jQuery.extends()
jQuery.fn.extends()
復制代碼 代碼如下:
jQuery.prototype = jQuery.fn = jQuery.fn.init.prototype
新聞熱點
疑難解答
圖片精選