誰能告訴我這兩種擴展函數有什麼區別嗎?謝謝。jquery有或沒有extend關鍵字
$.fn.alertWhileClick= function() {
alert($(this).val());
}
$.fn.extend({
alertWhileClick:function(){
alert($(this).val());
}
});
誰能告訴我這兩種擴展函數有什麼區別嗎?謝謝。jquery有或沒有extend關鍵字
$.fn.alertWhileClick= function() {
alert($(this).val());
}
$.fn.extend({
alertWhileClick:function(){
alert($(this).val());
}
});
這兩種形式是等價的。第一個通過賦值向jQuery原型添加方法,第二個通過調用$.fn.extend()
,這與$.extend()的方法相同。
的文件實際上是一個有點誤導這裏,因爲它說:
如果只有一個參數提供給
$.extend()
,這意味着目標省略 說法。在這種情況下,假設jQuery對象本身是 是目標。
但是實際發生的是更多的東西一樣:
如果只有一個參數提供給
$.extend()
,這意味着目標省略 說法。在這種情況下,對象$.extend()
被應用於(即,方法內與this
相關聯的對象)是 被假定爲目標。
因此,呼籲$.extend()
帶一個參數延伸$
,並呼籲$.fn.extend()
帶一個參數延伸$.fn
。
源代碼的相關部分說:
jQuery.extend = jQuery.fn.extend = function() {
// [...]
// extend jQuery itself if only one argument is passed
if (length === i) {
target = this; // Note: 'this', not 'jQuery'.
--i;
}
// [...]
};
答案像你這樣英俊! – 2013-02-27 14:48:40
http://api.jquery.com/jQuery.extend/ – edhedges 2013-02-27 14:26:06
嗨,edhedges,這個人是擴展'$'或'Jquery'對象方法。例如'$ .alertWhileClick()',謝謝。 – 2013-02-27 14:29:56
@Joe,是什麼讓你覺得這兩種形式有區別? – 2013-02-27 14:33:30