猿问

jquery中的原型链

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函数还能够执行

撒科打诨
浏览 1069回答 3
3回答

一只甜甜圈

原型中this指向的是实例对象,each里return this来返回这个对象,从而实现链式调用

梵蒂冈之花

链式编程return this

繁星淼淼

因为你return的是this,别说两个了,10个也可以
随时随地看视频慕课网APP

相关分类

JQuery
我要回答