Maybe a bad title, but this is my problem:
I’m building a framework to learn more about javascript. And I want to use “”jQuery”” style.
How can I make a function where the () is optional?
$("p").fadeOut(); //() is there
$.each(arr, function(k, v) {...}); //Dropped the (), but HOW?
This is what I have come up with, but it don’t work:
$2DC = function(selector)
{
return new function() {
return {
circle : function()
{
//...
}
}
}
}
$2DC("#id1"); //Work
$2DC("#id2").circle(); //Work
$2DC.circle(); //DONT WORK
$is really just an alias for thejQueryfunction. You can call the function with:jQuery("p");or$("p");but remember, in JavaScript you can attach “stuff” directly to functions.
This is how (conceptually) jQuery’s
eachfunction is defined.And so now
jQuery.eachis a function that can be called like this:or the more familiar
So for your particular case, to support:
You’d have to add the
circlefunction directly to$2DC: