Skip to content

Instantly share code, notes, and snippets.

@artemhp
Last active August 15, 2019 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artemhp/3122debde0e923c0b7fc2b37be650796 to your computer and use it in GitHub Desktop.
Save artemhp/3122debde0e923c0b7fc2b37be650796 to your computer and use it in GitHub Desktop.
Method Chain
const jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context );
}
jQuery.fn = jQuery.prototype = {
pushStack: function (elem) {
const ret = elem;
ret.prevObject = this;
return ret;
},
find: function (selector) {
return this.pushStack(jQuery(selector, this[0]));
},
end: function (selector) {
return this.prevObject
}
};
jQuery.fn.init = function( selector, context) {
const node = context || document;
this[ 0 ] = node.querySelector(selector);
return this;
}
jQuery.fn.init.prototype = jQuery.fn;
console.log(jQuery('body').find('main').find('div'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment