How can jquery $ act as an object as well as a function in javascript?
javascript
Solution
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:
var f = function() {};
f.h = function(x) {
console.log(x);
};
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 of `selector` which, off of which, we can run methods such as `.html`, `.css`, and `.toggle` to name a few.
Problem
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?