jq--CDN
jQ -- jQuery源碼解析
1、jQuery.extend() 與 jQuery.fn.extend()
把jQuery看成一個封裝js類 這樣好理解
$.extend是擴展的jQuery這個類 為jQuery類添加類方法 可以理解為靜態方法 只跟這個類本身有關 跟具體的實例化對象是沒關係的。
jQuery.fn.extend() 拓展的是jQuery對象(原型)的方法 得用在jQuery對象上面
區別
兩者調用方式不同
jQuery.extend() 由傳入的全局函數來調用 主要是用來拓展全局函數 如$.init() $.ajax();
jQuery.fn.extend() 由具體的實例對象來調用 可以用來拓展個選擇器 例如$.fn.each();
兩者主要功能作用不同
jQuery.extend(object) 為擴展jQuery類本身 為自身添加新的方法
jQuery.fn.extend(object) 給jQuery對+B187象添加方法
(function($) {
$.extend({
speakExtend:function(e) {
alert("$.extend"+" === "+ e);
}
});
$.fn.extend({
speakExtend:function(e) {
// 此處沒有必要將this包在$號中如$(this),因為this已經是一個jQuery對象。
// $(this)等同於 $($('#element')); 直接使用 this.
alert("$.fn.extend"+" === "+e);
}
})
})(jQuery);
$.speakExtend("靜態方法");
$(".main").speakExtend("對象專屬");
jQ -- 插件封裝
編寫一個jQuery插件就是給jQuery.fn加入新的功能屬性,此處添加的對象屬性的名稱就是你插件的名稱。
在插件的範圍裏, this關鍵字代表了這個插件將要執行的jQuery對象, 這裏容易產生一個普遍的誤區,因為在其他包含callback的jQuery函數中,this關鍵字代表了原生的DOM元素。誤將this關鍵字無謂的包在jQuery中,
(function ($) {
$.fn.myPlugin = function () {
//此處沒有必要將this包在$號中如$(this),因為this已經是一個jQuery對象。
//$(this)等同於 $($('#element'));
this.fadeIn('normal', function () {
//此處callback函數中this關鍵字代表一個DOM元素
});
};
})(jQuery);
$('#element').myPlugin();
實現簡單插件
(function ($) {
$.fn.maxHeight = function () {
var max = 0;
this.each(function () {
max = Math.max(max, $(this).height());
});
return max;
};
})(jQuery);
var tallest = $('div').maxHeight(); //返回高度最大的div元素的高度
維護Chainability
很多時候,一個插件的意圖僅僅是以某種方式修改收集的元素,並把它們傳遞給鏈中的下一個方法。 這是jQuery的設計之美,是jQuery如此受歡迎的原因之一。 因此,要保持一個插件的chainability,你必須確保你的插件返回this關鍵字。
(function ($) {
$.fn.lockDimensions = function (type) {
return this.each(function () {
var $this = $(this);
if (!type || type == 'width') {
$this.width($this.width());
}
if (!type || type == 'height') {
$this.height($this.height());
}
});
};
})(jQuery);
// 返回 this 後續調用 css()
$('div').lockDimensions('width').css('color', 'red');
默認值和選項
對於比較複雜的和提供了許多選項可定製的的插件,最好有一個當插件被調用的時候可以被拓展的默認設置(通過使用$.extend)。
在這個例子中,調用tooltip插件時覆寫了默認設置中的location選項,background-color選項保持默認值;
(function ($) {
$.fn.tooltip = function (options) {
//創建一些默認值,拓展任何被提供的選項
var settings = $.extend({
'location': 'top',
'background-color': 'blue'
}, options);
return this.each(function () {
// Tooltip插件代碼
});
};
})(jQuery);
$('div').tooltip({
'location': 'left'
});
插件方法
在任何情況下,一個單獨的插件不應該在jQuery.fn,這是不被鼓勵的,因為$.fn命名空間混亂。
應該收集對象文本中的所有插件的方法,通過傳遞該方法的字符串名稱給插件以調用它們。
(function ($) {
var methods = {
init: function (options) {
// this
},
show: function () {
// is
},
hide: function () {
// good
},
update: function (content) {
// !!!
}
};
$.fn.tooltip = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.tooltip');
}
};
})(jQuery);
$('div').tooltip(); // 調用init方法
$('div').tooltip({
foo: 'bar'
}); // 調用init方法
$('div').tooltip('hide'); // 調用hide方法
$('div').tooltip('update', 'This is the new tooltip content!'); // 調用Update方法
事件
允許綁定事件命名空間。如果你的插件綁定一個事件,一個很好的做法是賦予此事件命名空間。
具體詳情在下面鏈接中
數據
通常在插件開發的時候,可能需要記錄或者檢查你的插件是否已經被初始化給了一個元素。 使用jQuery的data方法是一 個很好的基於元素的記錄變量的途徑。
具體詳情在下面鏈接中
總結和最佳做法
始終包裹在一個封閉的插件:
(function($) {
/* plugin goes here */
})(jQuery);
在插件的功能範圍內不要冗餘包裹this關鍵字
除非插件返回特定值,否則總是返回this關鍵字來維持chainability 。
傳遞一個可拓展的默認對象參數而不是大量的參數給插件。
不要在一個插件中多次命名不同方法。
始終命名空間的方法,事件和數據。
封裝插件--1
封裝插件--2