文章詳情頁
javascript - jquery中的原型鏈
瀏覽:108日期:2023-04-14 15:41:50
問題描述
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構造函數jQuery().each().each()
上面是一段jQuery源碼,我的問題是為什么代碼最后一行的第二個each函數還能夠執行
問題解答
回答1:原型中this指向的是實例對象,each里return this來返回這個對象,從而實現鏈式調用
回答2:兩個each跟一個each效果一樣,對象都是jQuery
回答3:因為你return的是this,別說兩個了,10個也可以
鏈式編程
return this
標簽:
JavaScript
相關文章:
排行榜
