Modify ↓
Ticket #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: | ||
| 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
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
Note: See
TracTickets for help on using
tickets.

I just added this! You can give this a try in the latest SVN version: jQuery.extend( {
},{option2: 'bar'});
Doing just this will add it to the jQuery object: jQuery.extend({
});
And you can do this to add multiple properties to the jQuery prototype: jQuery.fn.extend({
});
Hope this helps!