what is the usage of jQuery.extends

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amskape
    New Member
    • Apr 2010
    • 56

    #1

    what is the usage of jQuery.extends

    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
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I found the examples quite straightforward . you take in 2+ objects and get back 1 merged object. (it does not seem to have anything to do with inheritance)

    Comment

    • amskape
      New Member
      • Apr 2010
      • 56

      #3
      Hi Dormilich, 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,unmodi fied 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(obje cts), in that time we need
      to extend the deafault object with our own

      eg:
      Code:
      (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 :
      Code:
      $(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'.
      Code:
      $(document).ready(function() {
        $('div').say({word: 'Goodbye'});
      });
      Am I right Dormilich, If my understanding has any
      problem advise me

      Regards
      Anes P.A
      Last edited by Dormilich; Dec 1 '10, 11:50 AM. Reason: please use [CODE] [/CODE] tags when posting code

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I’m not much of a jQuery user …

        Comment

        Working...