Possible Duplicate:
How can jQuery behave like an object and a function?
In jquery we can use $ as a function as well as a namespace. for example both of these use-cases are valid.
$("div").html("<b>Wow!</b> Such excitement...");var trimmed = $.trim(someString);
How can one identifier $ act as an object as well as a function at the same time?
First things first:
jQuery(or its alias$) is a function. This is very important because functions are “first-class” objects in JavaScript. And since they are objects themselves, they have the ability to be given properties and methods just as any other object. For example:This is what allows jQuery to work its magic. In addition, through the use of inheritance, we have the potential to chain methods like you show in your first example.
$(selector)returns a jQuery “interface” (technically[object Object]) based on the value ofselectorwhich, off of which, we can run methods such as.html,.css, and.toggleto name a few.