One assigns a plugin to the jQuery prototype property. What’s the point of doing this when one doesn’t use ‘new’ with jQuery? There’s only ever one instance of jQuery isn’t there? So couldn’t plugins be assigned directly to the singleton jQuery object?
Share
Plugin methods must be automatically added to each new jQuery object that is created.
jQuery.fnis the same asjQuery.prototypeso assigning a plugin method tojQuery.fnis adding it to the jQuery prototype so whenever a new jQuery object is created, that method will automatically be available. Technically, it’s ajQuery.fn.initobject, but the idea is the same.Calling jQuery with a string like this:
creates a new jQuery.fn.init object which gets its prototype from jQuery.fn (where plug-in methods are defined).
Here’s the relevant jQuery code that shows a new object getting created: