Bug Tracker

Opened 17 years ago

Closed 17 years ago

Last modified 16 years ago

#70 closed feature (fixed)

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.

Change History (1)

comment:1 Changed 17 years ago by john

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!

Note: See TracTickets for help on using tickets.