Function in a function?

+2 Landon Luman · November 25, 2014
So jQuery is a widely used JavaScript source, but I was wondering how there are functions that can run like that.

So it is clear that the $ mark is actually the name of the function, but how are there functions after that?


$('element id').draggable();

I would assume draggable() is a function of it's own, but is it like, in the $() function


Post a Reply


Oldest  Newest  Rating
+3 Samuel Oloruntoba · November 29, 2014
Actually the $ is a function object so it's got methods of its own, the .draggable() is actually a method of the $ object
+1 William Abboud · December 6, 2014
Functions in JavaScript are objects. For example you can do something like:

function hello(){

and then 

hello.whatever = "here is some text";

if you log out - console.log(hello.whatever); 

you will get - here is some text 

In jQuery the dollar sign is a variable namespace. Dollar $ or jQuery will call you the same function


are both equivalent

$() is a function object on which you can pass a parameter say an element to search for and then that will return a jQuery wrapper object to wrap the dom element and via chaining attach methods to it. For example this common scenario


The above translates to find the element with the id of box find all anchor elements and colorize them.

For more of the internal working of jQuery either look at the source or the official docs or search paul irish jquery on YouTube
0 Andrew McCombs · December 6, 2014
Just to let you know that you can also do


if your using JavaScript and jQuery that's what I like to do. Just letting you know :) 
  • 1



A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.