文章详情页
javascript - jquery中的原型链
浏览:130日期: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. node.js - express请求的具体方法2. node.js - AWS配置nginx的反向代理不生效问题3. node.js - 关于Node爬虫爬小说的问题4. html5 - 有可以一次性把所有 css外部样式转为html标签内style=" "的方法吗?5. web - Rails3使用form_for时出现undefined method `*_path’错误。6. node.js - 关于mongoose方法的回调函数的参数问题,如何知道参数个数以及参数代表什么含义呢?7. node.js - react应用加了react-redux后webpack打包后页面显示空白,也没有报错8. node.js - 引入node-fetch后,使用webpack的时候报错!9. 在启动后的90秒内,Heroku- Web进程未能绑定到$ PORT。TooTallNate Websockets10. node.js - 如何使用mongoose连接数据库中已经存在的一个集合。
排行榜
