Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool 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

  • #2
    Senior Coder
    Join Date
    Feb 2009
    Location
    Ilkley, West Yorkshire, UK
    Posts
    2,957
    Thanks
    9
    Thanked 727 Times in 721 Posts
    .extend allows the merging of two objects.

    A relatively common use in a custom jQuery plugin would be to merge an object containing defaults for a function with options selected by the user. So, if the former are held in an object variable called defaults, and the latter in an object variable called options (typically passed as an argument to the function), then:

    Code:
    $.extend( defaults, options )
    would merge the two to create an amended defaults object including any options selected.

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Posts
    149
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Are you using jQuery.extend properly?

    jQuery's extend function is pretty powerful for merging objects and it is the hinge that plugins rely on to operate. It is very easy to use, but if you don't understand exactly what it does, it can come back and bite you. The first case for using the method is to extend the jQuery object itself. Calling it on jQuery or jQuery.fn will add functions to the jQuery namespace or jQuery objects, respectively. The second case is to merge objects together. You can do this by calling extend with multiple parameters. The first parameter is considered to be the base and all subsequent ones are merged into it in order. The updated object is also returned. The important thing to be aware of is that the first parameter will be updated by this call. For example, if you have an object in your plugin that has its defaults stored in an object and you pass that in as the first argument, then it will by updated by the parameters passed in. The wrong way to do it, causing your defaults to be rewritten: [js] var defaults = { some: 'option' }; function myPlugin(user_options) { var options = $.extend(defaults, user_options); ... } [/js] Instead, the safe way to call extend in that instance is like this: [js] var options = $.extend({}, defaults, user_options); [/js] That will cause the user options to override the defaults without rewriting them and store it in another variable for your use.


    Reference Site : http://codingfrontier.com/are-you-us...xtend-properly


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •