文章詳情頁
javascript - jquery中的原型鏈
瀏覽:123日期: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
相關文章:
1. sql語句 - mysql中關聯表查詢問題2. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。3. javascript - 求解答:實例對象調用constructor,此時constructor內的this的指向?4. javascript - 如何將一個div始終固定在某個位置;無論屏幕和分辨率怎么變化;div位置始終不變5. css - chrome下a標簽嵌套img 顯示會多個小箭頭?6. html - vue項目中用到了elementUI問題7. javascript - 有什么比較好的網頁版shell前端組件?8. javascript - 原生canvas中如何獲取到觸摸事件的canvas內坐標?9. mysql updtae追加數據sql語句10. javascript - iframe 為什么加載網頁的時候滾動條這樣顯示?
排行榜
