Bug Tracker

Modify

Ticket #70 (closed feature: fixed)

Opened 7 years ago

Last modified 6 years ago

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:
Blocking: Blocked by:

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

comment:1 Changed 7 years ago by john

  • Status changed from new to closed
  • Version set to 1.0
  • Resolution set to fixed
  • Milestone set to 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!

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.