1

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?

Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
wwaawaw
  • 6,867
  • 9
  • 32
  • 42
  • 1
    Checkout http://stackoverflow.com/questions/5527031/jquery-fn-namespace and http://stackoverflow.com/questions/11542017/what-does-fn-in-jquery-stand-for – Jonathan Lonowski Oct 31 '12 at 03:44
  • 1
    Also http://stackoverflow.com/questions/1755080/why-jquery-do-this-jquery-fn-init-prototype-jquery-fn – Musa Oct 31 '12 at 03:45
  • https://github.com/jquery/jquery/blob/master/src/core.js#L84 – Nemoden Oct 31 '12 at 03:46

2 Answers2

1

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 ;)

Kpower
  • 1,237
  • 3
  • 11
  • 19
1

From the jquery source:

jQuery.fn = jQuery.prototype = {

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

Zirak
  • 38,920
  • 13
  • 81
  • 92
  • Fine, but where do they get the string "fn" from? That evokes the word "function" in my mind, which makes no sense. Wouldn't `jQuery.pt` make more sense? – wwaawaw Oct 31 '12 at 08:06
  • @adlwalrus I have no idea. It may be an inside-story or something of the sorts. I'll try digging in the older sources. Edit: Maybe it's to denote that there should only be functions there? – Zirak Oct 31 '12 at 09:18
  • @adlwalrus Asked on irc, they seem to think as I, so I [tweeted](https://twitter.com/zirakertan/status/263576846350823424) John Resig. Hopefully we'll have an answer. – Zirak Oct 31 '12 at 09:45