谁能告诉我这两种扩展函数有什么区别吗?谢谢。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