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  :-*

Link to comment
Share on other sites

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'});
});

Link to comment
Share on other sites

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  :-*

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.