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?

3

2 Answers 2

1

From the jquery source:

jQuery.fn = jQuery.prototype = {

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

3
  • 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
    Commented Oct 31, 2012 at 8: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
    Commented Oct 31, 2012 at 9:18
  • @adlwalrus Asked on irc, they seem to think as I, so I tweeted John Resig. Hopefully we'll have an answer.
    – Zirak
    Commented Oct 31, 2012 at 9:45
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 ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.