[Probably] The Most Elegant Solution
First off, jQuery uses a pattern which is closer to a Monad, a Factory, or a combination of both. Nevertheless, here's what I've been using in my projects because the pattern, itself, is so loosely coupled to whatever class you'd like to utilize:
;(function (undefined) {
if (undefined) return;
var ENV = this;
var Class = function Class() {
var thus = this;
function find(data) {
console.log('@find #data', data);
return this;
}
function show(data) {
console.log('@show #data', data);
return this;
}
// export precepts
this.find = find;
this.show = show;
return this;
};
var Namespace = ENV['N'] = new (function Namespace(Class) {
var thus = this;
var Ns = Class.apply(function Ns(data) {
if (this instanceof N) {
return new Namespace(Class);
}
return Ns.find.apply(Ns, arguments);
});
return Ns;
})(Class);
}).call(window || new function Scope() {});
var n = N('#id').show(450);
var m = new N();
m('#id')('.curried').show('slow');
console.log(n !== m); // >> true
Basically, you can use it as a function, an object, and use the new keyword to construct another unique object/function. You can use this to enforce an arbiter method (default, like the find method above), or use different methods based upon what parameters are input. For instance, you can do something like:
var elementsList = N('#id1')('#id2')('#otherSpecialElement').each(fn);
-- OR --
var general = N('.things');
var specific = general('.specific')('[data-more-specific]').show();
The above would, for instance, accumulate a nodelist of multiple elements (1st expression), or drill down to one specific element (2nd).
Hope this helps
test(...)is not the same asnew test(...). :)