Jump to content

what is the actual usage of jQuery.extends


logicslab

Recommended Posts

Hi pals,

I am in a straight forward doubt. I use jQuery for a long time , but not use jQuery extends method, do any one can

explain it's purpose and need . I don't get not much from reading jQuery Manual. So if you have a Good idea reply me with a Good explanation and example....I can understand

user can create their own custom functions, pls explain ...

 

Thankfully

Anes P.A  :-*

I should create an example. Allot of jQuery plugins use it to define default options,  which can latter be merged with user inputted options. eg;

 

(function($) {
  $.fn.say = function(options) {
    var ops = {
      'word' : 'Hello'
    };
    return this.each(function() {
      if (options) {
        // here I merge the users inputted 'options' object
        // with my 'ops' object.
        $.extend(ops, options);
      }
      $(this).html(ops.word);
    });
  };
})(jQuery);

 

This is just a simple jQuery plugin that when called like.....

 

$(document).ready(function() {
  $('div').say();
});

 

Would make all div's within a document contain the html 'Hello'. Now, using $.extend() we can override the word hello with 'goodbye'.

 

 

$(document).ready(function() {
  $('div').say({word: 'Goodbye'});
});

Hi Thorpe, I got some idea about jQuery.extends , thanks for your help , pls verify my idea as  I know depicted below:

$.extend(target,objects...) --->Extends the target

object with one or more specified objects.Returns the

original,unmodified object.

 

It's used mainly in creating plugins. For example,

in a plugin function there need some default

objects, some time we need to pass our own

preference(objects), in that time we need

to extend the deafault object with our own

 

eg: (function($) {

  $.fn.say = function(options) {

    var ops = {

      'word' : 'Hello'

    };

    return this.each(function() {

      if (options) {

        // here I merge the users inputted 'options' object

        // with my 'ops' object.

        $.extend(ops, options);

      }

      $(this).html(ops.word);

    });

  };

})(jQuery);

 

and call this as :

$(document).ready(function() {

  $('div').say();

});

Would make all div's within a document contain the html 'Hello'.

Now, using $.extend() we can override the word hello with 'goodbye'.

$(document).ready(function() {

  $('div').say({word: 'Goodbye'});

});

 

Am I right Thorpe, If my understanding has any

problem advise me

 

Regards

Anes P.A  :-*

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.