I am trying to understand the jquery plugin syntax, because I want to merge two plugins into one. The blinker that also needs to be able to stop de interval or run a number of times.
Anyway, is this syntax the same as
jQuery.fn.extend({
everyTime: function(interval, label, fn, times) {
return this.each(function() {
jQuery.timer.add(this, interval, label, fn, times);
});
},
oneTime: function(interval, label, fn) {
return this.each(function() {
jQuery.timer.add(this, interval, label, fn, 1);
});
},
this
$.fn.blink = function(options)
{
because it looks like the first(without =) is a way to set multiple methods at once. Is this right? Also while I am here What would be the reason to add the elements and some logic to the jquery object?
jQuery.extend({
timer: {
global: [],
guid: 1,
dataKey: "jQuery.timer",
(this is from the timer plugin)
usage:
$.abc()
. (No selector required like$.ajax()
.)usage:
$('.selector').xyz()
. (Selector required like$('#button').click()
.)Mainly it is used to implement
$.fn.each()
.I hope it helps.
This blog post have nice description:
Quotes:
As a general rule, you should extend the jQuery object for functions and the jQuery.fn object for methods. A function, as opposed to a method, is not accessed directly from the DOM.
Actually, there is none apart from their base reference. In the jQuery source, you can read:
So how does it work? The documentation reads:
It's just a for-in-loop that copies properties, pimped up with a flag to recurse nested objects. And another feature:
So if the function is called on
jQuery
itself (without explicit target), it will extend the jQuery namespace. And if the function is called onjQuery.fn
(without explicit target), it will extend the jQuery prototype object where all the (plugin) methods are located.points to the jQuery.prototype and lets access dom elements through "this". Now u may use
$(selector).something();
So this works as plugin function like$(selector).css();
adds a property or function to the jQuery object itself and u cannot use "this" for dom access Now u may use it as
$.something()
; this works as a utility function as$.trim()
but
allows adding more than 1 function at the same time.Also they can be used to merge two object literals in case we provide more than one objects.
jQuery.extend is used to extend any object with additional functions, but jQuery.fn.extend is used to extend the jQuery.fn object, which in fact adds several plugin functions in one go (instead of assigning each function separately).
jQuery.extend:
jQuery.fn.extend: