I am trying to set up library like jQuery simply for learning purposes. I have it working decently well, and I can do method chaining as needed. The problem I am having is being able to call a method with the class’ parenthesis:
What I have to do:
$foo().get('id1');
What I would like to do:
$foo.get('id1');
Here is the current javascript:
(function( window ) {
var document = window.document;
var fooTools = (function() {
var fooTools = function( selector ) {
return new fooTools.base.init( selector );
};
fooTools.base = fooTools.prototype = {
init: function( selector ) {
if( !selector ) {
return this;
}
if( selector ) {
this[0] = selector;
this.length = 1;
return this;
}
},
get: function( id ) {
return document.getElementById( id );
},
//..other methods
};
fooTools.base.init.prototype = fooTools.base;
return fooTools;
}());
window.fooTools = window.$foo = fooTools;
}( window ));
Currently it works just fine, but if I do not include the parenthesis I get an error that the .get() method does not exist. I still want to maintain the ability to use parenthesis for other methods, so i just want it be optional. Thanks!
change:
to:
Live Example
EDIT
Updated Example