I realize that one is a prototype can someone tell me the difference between these two?
$.addMessage = function () {
and
$.fn.addMessage = function () {
From what I can see one extends JQuery and the other JQueryStatic in typescript
interface JQuery {
addMessage();
}
interface JQueryStatic {
addMessage();
}
If you add a function as
you can later access the function as
Semantically, the function has no relation to specific jQuery instances, and may be regarded to as an equivalent of a static method (belongs to a class) in Java.
If you add a function as
Since jQuery defines $.fn as an alias to $.prototype, this is equivalent to
Javascript automatically assigns
Class.prototypeas a prototype of the objects created withnew Class().$(...)usesnew $(...)internally, meaning it does create new objects with$as the constructor. This means that$.fn.addMessagecan also be accessed asSemantically,
$.fn.addMessageis related to specific jQuery instances. It means thatthisinside theaddMessagefunction does not refer to$.fn, but rather to$(...).$.fnmay thus be regarded to as an equivalent of an instance method (belongs to an instance) in Java.In other words, if you declare your function as
, you use it as
If you declare it your function as
, you use it as