views:

2920

answers:

2

Hi,

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)

thanks, Richard

+10  A: 

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:

var obj = { x: function() {} }

jQuery.extend(obj, { y: function() {} });

// now obj is an object with functions x and y

jQuery.fn.extend:

jQuery.fn.extend( {
        x: function() {},
        y: function() {}
});

// creates 2 plugin functions (x and y)
Philippe Leybaert
thanks, thats clear about assign multiple objects, but I am still not sure when one should use jquery.extend. Why would you pass something that you do in fn.extend to the jquery.extend? I can also post a new question for this, because I asked two questions.
Richard
jQuery.extend() is actually a convenience function that can be used in your own javascript code to extend objects in a generic way. It's part of the "utility" functions in jQuery (not used for DOM manipulation)
Philippe Leybaert
+2  A: 

Here is the fair answer to your question :)

Sarfraz
I read that one also. - one is used for functions and the other for methods -. I don't understand that completely. It's to vague for me.
Richard