var jQuery = function(global, factory) {
return new jQuery.fn.init();
}
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function() {
this.jquery = 3;
return this;
},
each: function() {
console.log('each');
return this;
}
}
jQuery.fn.init.prototype = jQuery.fn;
// init構(gòu)造函數(shù)
jQuery().each().each()
上面是一段jQuery源碼,我的問(wèn)題是為什么代碼最后一行的第二個(gè)each
函數(shù)還能夠執(zhí)行
溫故而知新,可以為師矣。 博客:www.ouyangke.com
原型中this指向的是實(shí)例對(duì)象,each里return this來(lái)返回這個(gè)對(duì)象,從而實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用
因?yàn)槟鉹eturn的是this,別說(shuō)兩個(gè)了,10個(gè)也可以