Skip to main content

Bug Tracker

Side navigation

#70 closed feature (fixed)

Opened July 15, 2006 10:23PM UTC

Closed July 16, 2006 12:06AM UTC

Last modified June 21, 2007 04:27AM UTC

Add an merge object method to jQuery core

Reported by: Jörn Owned by:
Priority: minor Milestone: 1.0
Component: core Version: 1.0
Keywords: Cc:
Blocked by: Blocking:
Description

I saw the idea of merging objects to overwrite default options some time ago in a plugin...

Many plugins have default values that can be overwritten by the client. A convienent approach is to use an object literal to define defaults and merge the defaults with options given by the client.

Example:

defaults = {
  option1: 1,
  option2: "foo"
}
options = {
  option2: "bar"
}
// merge these
$.mergeObject(defaults, options);

// Would result in options being unchanged an
// defaults with option1 equals 1 
// and option2 equals "bar"

The mergeObject function is very small:

/**
 * Merges two objects, overwriting all properties
 * of the defaults with properties from
 * the options, if present.
 */
jQuery.mergeOptions = function(defaults, options) {
	for(var i in options) {
		defaults[i] = options[i];
	}
}

Having this in jQuery core and adding some documention about this could by quite a help for plugin developers.

Attachments (0)
Change History (1)

Changed July 16, 2006 12:06AM UTC by john comment:1

milestone: → 1.0
resolution: → fixed
status: newclosed
version: → 1.0

I just added this! You can give this a try in the latest SVN version:

jQuery.extend( {

option1: 1, option2: 'foo'

},{option2: 'bar'});

Doing just this will add it to the jQuery object:

jQuery.extend({

foo: 'bar'

});

And you can do this to add multiple properties to the jQuery prototype:

jQuery.fn.extend({

method1: 'one',

method2: 'two'

});

Hope this helps!