I am trying to create a mini jQuery clone that can support a chain of methods. So far I have come up with this piece of code:
var $ = (function () {
var elements = [];
function methodOne() {
console.log('Method 1');
return this;
}
function methodTwo() {
console.log('Method 2');
return this;
}
return {
methodOne: methodOne,
methodTwo: methodTwo
};
}());
When the page loads, the variable is $
populated with the jQuery cloning object returned by IIFE.
My question is: how can I make an object $
be called directly as a function, while preserving the functionality of the method chain?
Currently I can use $.methodOne().methodTwo()
, but I can not use $('some parameter').methodOne().methodTwo()
, as jQuery does.
source
share