search

Home  >  Q&A  >  body text

Prototype chain in 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()

The above is a piece of jQuery source code. My question is why the second each function in the last line of the code can still be executed

欧阳克欧阳克2723 days ago704

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-07-05 10:38:08

    This in the prototype points to the instance object, return this in each to return this object, thereby realizing chain calls

    reply
    0
  • 滿天的星座

    滿天的星座2017-07-05 10:38:08

    Two eachs have the same effect as one each, and the objects are all jQuery

    reply
    0
  • 天蓬老师

    天蓬老师2017-07-05 10:38:08

    Because what you are returning is this, let alone two, 10 will do too

    reply
    0
  • 世界只因有你

    世界只因有你2017-07-05 10:38:08

    Chain programming

    return this

    reply
    0
  • Cancelreply