Alex Rivera | Logout

How does jQuerys $.each() work?

Asked 2012-01-25T17:10:42.427
9

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
Edit
Report

1 Answer

-1

Try it like this:

$2DC = (function(selector)
{
    return new function() {
        return {
            circle : function()
            {
                //...
            }
        }
    }
})();

this way the $2DC is the object returned by the function and not the function itself.

answered 2012-01-25T17:17:03.230

Your Answer