1

I saw a post out there:

what does $.fn mean

However I still don't understand it. Can someone explain this in very simple terms to me. Why did they choose to specify it this way?

Community
  • 1
  • 1
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • which part did you not understand exactly ? The prototype part, the constructor part or something else ? –  Sep 10 '12 at 14:25
  • To understand prototypes a little better have a read of this -> http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/ its long but well worth a read – Manse Sep 10 '12 at 14:39

2 Answers2

5

$.fn is just an alias for jQuery.prototype. To write a jQuery plugin, such as DataTables, one usually starts by adding a new function property to $.fn.

In simpler terms, when you write $.fn.pluginName, you are extending jQuery's prototype, by adding a new function called pluginName to it. This way, you can call it using, for example, $("#myElement").pluginName(). That's basically what DataTables does, it provides an extension to jQuery called dataTablesExt.

Now, this plugin has several properties. One of them, is called afnSortData (which you could also refer to using jQuery.prototype.dataTableExt.afnSortData). This way, the plugin properties are scoped to the dataTableExt object/plugin. DataTables could have opted to define it in the global namespace, but this way, someone could overwrite afnSortData with, say, {} and break the plugin.

João Silva
  • 89,303
  • 29
  • 152
  • 158
1

In jQuery, the fn property is just an alias to the prototype property.

The jQuery identifier (or $) is just a constructor function, and all instances created with it, inherit from the constructor's prototype.


Source: What does jQuery.fn mean?

So it stand just for jQuery.prototype.

$.fn === jQuery.prototype

What .prototype is?

In JavaScript, the prototype is the mechanism which provides inheritance.

Example: String.prototype refers to the prototype object of Strings, which contains (among other things) methods which can be invoked on any String.

Community
  • 1
  • 1
Andrea Turri
  • 6,480
  • 7
  • 37
  • 63
  • I read the post and also linked to it in my question. However what you said I can't understand. I don't know what the prototype property is so knowing it's an alias still doesn't tell me anything. Thanks – Samantha J T Star Sep 10 '12 at 14:28
  • I added the explanation of it as well. Just inheritance. – Andrea Turri Sep 10 '12 at 14:35