这个问题大家看看吧

来源:2-3 静态与实例方法共享设计

我觉得可以啊

2016-08-09 21:41

init: function(selector) {
this.selector = selector;
return this;
},
为什么要return this呢?
这是jquery源码里的问题

var $$ = ajQuery = function(selector) {

    return new ajQuery.fn.init(selector);

}


ajQuery.fn = ajQuery.prototype = {

init: 

}


写回答 关注

2回答

  • stZhang1998
    2016-08-16 20:01:18
    已采纳

    return this;可以让JQuery形成一个链式的使用结构。在jQuery对象中,this指向本身的jQuery对象。

    我觉得可以啊

    new ajQuery.fn.init(selector) 关键是return了之后 又new了,既然return this 了,为什么还要用new呢 这样相当于new (return this)

    2016-11-01 20:41:16

    共 2 条回复 >

  • 慕用5482051
    2018-11-12 13:58:24

    w刚开始看也是这个疑问

jQuery源码解析(架构与依赖模块)

由浅入深地剖析jQuery库的设计与实现,揭开框架背后的秘密

84050 学习 · 290 问题

查看课程

相似问题