Javascript Function like Objects i.e. "$" can be used as a function e.g. $() as well as an object $.

Posted by Drew on Stack Overflow See other posts from Stack Overflow or by Drew
Published on 2010-06-01T21:02:27Z Indexed on 2010/06/01 21:33 UTC
Read the original article Hit count: 137

Filed under:

Questions in the title. I've always wondered and failed to find out from the jQuery source. How this is done.

To reiterate. In jQuery: how does the "$" become a function e.g."$()" as well as an object "$."

I can create it one way OR the other like so...

var $ = function(){
    return {each:function(){console.log("Word")}}
}
// $.each(); FAIL!
$().each(); // Word


var $ = {
    each:function(){console.log("Word")}
}
$.each(); // Word
//$().each(); FAIL!

© Stack Overflow or respective owner

Related posts about JavaScript