wwaawaw
wwaawaw

Reputation: 7137

Why does jQuery alias its prototype to `fn`?

Just look at any jQuery plugin's source code. It's always

$.fn.pluginName = function() {
    foo('bar');
    return baz;
};

Where does the fn name come from, and is it just a simple alias to $.prototype, or does it serve some other purpose(s), as well?

Upvotes: 1

Views: 665

Answers (2)

Zirak
Zirak

Reputation: 39838

From the jquery source:

jQuery.fn = jQuery.prototype = {

It's just an alias, a shortcut, just like how jQuery and $ are the same.

Upvotes: 1

Kpower
Kpower

Reputation: 1257

fn is the part of the jquery library controls element selection so $.foo = function () {}; will not exist for $('#a').foo(); but $.fn.foo = function () {}; will. Similarly, $.fn.foo will not exist if you try $.foo();. It also makes the this keyword relevant to the current element used. And your comment about "any jquery plugin having fn" isn't true ;)

Upvotes: 1

Related Questions