2

Possible Duplicate:
What does jQuery.fn mean?

Part of the jQuery source code states:

jQuery.fn = jQuery.prototype = {

Given this, wouldn't

$.fn.adamPlugin = function(options) {  
   return this.each(function() {   });  
};

Be identical to

$.prototype.adamPlugin = function(options) {  
   return this.each(function() {   });  
};  

If so, what's the point of $.fn? Adding things to a prototype is fairly common in JavaScript, so I can't quite understand why the jQuery folks would try to abstract this away.

Community
  • 1
  • 1
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • 2
    I'd imagine the short-hand allows them to make the library smaller... also, I hear they're big proponents of _writing less_. – canon Jan 31 '12 at 16:46
  • Why $ for jQuery? Replacing the one with the other is identical, too. – pimvdb Jan 31 '12 at 16:48
  • 1
    Paul Irish explains it a little [here](http://paulirish.com/2011/11-more-things-i-learned-from-the-jquery-source/) as well. – Andrew Jan 31 '12 at 16:50
  • @antisanity: That's probably it. In addition, a minifier could not rename the `prototype` property as it is a special name, but it could rename `fn`. – Felix Kling Jan 31 '12 at 16:50
  • @antisanity - that's it. It makes the library size smaller, and is friendlier to minifiers. Can you put that as an answer before this question gets closed? – Adam Rackis Jan 31 '12 at 16:52
  • Look at the source code line 97 of 1.7.1 – noob Jan 31 '12 at 16:52
  • *"can't quite understand why the jQuery folks would try to abstract this away"* jQuery seems to want to abstract nearly *everything* away. This is unfortunate. –  Jan 31 '12 at 17:03
  • I don't think it is a duplicate. The other question asks *what* it is, which Adam knows already. The question is here is *why* it is used and what's the advantage. Though I think that this question is better asked directly to the developer team. – Felix Kling Jan 31 '12 at 17:04
  • @Felix - you're right. I think antisanity nailed the answer. If this gets re-opened I think that's the accepted answer. – Adam Rackis Jan 31 '12 at 17:06

1 Answers1

3

It's just a convenient alias for prototype that's all.

Jivings
  • 22,834
  • 6
  • 60
  • 101