View Full Version : what is the usage of jQuery.extends

11-30-2010, 02:16 PM
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 ...

Anes P.A :p

11-30-2010, 04:18 PM
.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:

$.extend( defaults, options )

would merge the two to create an amended defaults object including any options selected.

11-30-2010, 06:34 PM
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: var defaults = { some: 'option' }; function myPlugin(user_options) { var options = $.extend(defaults, user_options); ... } Instead, the safe way to call extend in that instance is like this: var options = $.extend({}, defaults, user_options); 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-using-jqueryextend-properly