文章详情页
javascript - jquery中的原型链
浏览:110日期: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
相关文章:
排行榜
