Rafalages
Rafalages

Reputation: 1426

What's difference between "$.FunctionName()" and "FunctionName()"?

what's the difference between a jQuery function

$.FunctionName = function(){ alert('Hello!') }

and normal javascript function?

function FunctionName(){ alert('Hello!') }

Upvotes: 3

Views: 153

Answers (2)

seriyPS
seriyPS

Reputation: 7102

There is no significant differences. Both functions will work the same.

If you want to create you own functions library, better way is create new class (named not $) like my_lib={} and then add functions to it like

my_lib.FunctionName = function(){ alert('Hello!'); }

Or

my_lib={
    FunctionName: function(){ alert('Hello!'); }
}

Upvotes: 0

meder omuraliev
meder omuraliev

Reputation: 186562

The former becomes a static method of the jQuery object. The latter becomes just a regular function.

The only difference, really is the owner of the function. The jQuery object/constructor owns the first method, while the window object owns the second method, assuming it wasn't defined in another function scope.

Generally, you do not usually do the first one unless you want to attach a specific method that's related to jQuery. If you have a custom application specific function do the latter.

Upvotes: 6

Related Questions