I've been developing a JavaScript library similar to Underscore.js and Jquery in the past few days. In both of those libraries they use an object which can accept parameters but can also have methods called on it: $("param").method(); or _("param").method();
I've been reading through the source code on both of these libraries trying to understand how they are implementing such a thing but have been unable to figure it out. I do not know the name of this type of closure so I've been unable to search for it.
Any help would be appreciated, I'm just trying to figure out how I can implement an object of this type into my library.
var lib = (function (param) {
var func = function () {
/// your code
return {
animate : function () {
// do the animation
return this;
}
}
return func;
})();
You could use
lib(function(){}).something();
lib("selector").something().something().something();
lib(DOMElement).something().something().something();
Defining
lib.prototype={
some: function() {}
};
allows you to use
lib("foo").some();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With