Skip to main content

Bug Tracker

Side navigation

#12573 closed feature (plugin)

Opened September 19, 2012 07:15PM UTC

Closed September 19, 2012 09:37PM UTC

Last modified September 19, 2012 09:52PM UTC

Better API for Extend

Reported by: Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.8.0
Keywords: Cc:
Blocked by: Blocking:

$.extend is a very useful function but it is hard to remember what is happening because depending on the arguments that you give it, it does completely different tasks.

Here is what I do to remember

ObjectUtils =  {
    deepCopy: function(obj){
        return $.extend(true, {}, obj);
    copy: function(obj){
        return $.extend({}, obj);
    update: function(base, override){
        //any keys in overrides that match keys in base will now
        //point to the override data.
        //any keys in overrides not in base will also be added to base
        //NOTE: no copies here! only updates to base
        $.extend(base, override);
    augment: function(base, extensions){
        var args = Array.prototype.concat.apply([{}], arguments);
        return $.extend.apply($, args);
    deepAugment: function(base, extensions){
        var args = Array.prototype.concat.apply([true,{}], arguments);
        return $.extend.apply($, args);

I know there are probably more things you can do with $.extend but these are very common tasks that could be highlighted with a simple wrapper function name.

Attachments (0)
Change History (6)

Changed September 19, 2012 07:32PM UTC by rwaldron comment:1

resolution: → wontfix
status: newclosed

This API has existed for many years and a lot of code is written that relies on it.

Changed September 19, 2012 09:27PM UTC by comment:2

You can keep the same API and add a thin wrapper around it like and I think code would be much more readable.

Changed September 19, 2012 09:28PM UTC by anonymous comment:3

ahem... what I meant to say is keep the same API and add a thin wrapper around $.extend like I did in my comment

Changed September 19, 2012 09:37PM UTC by dmethvin comment:4

resolution: wontfix
status: closedreopened

Changed September 19, 2012 09:37PM UTC by dmethvin comment:5

resolution: → plugin
status: reopenedclosed

Sure, you're welcome to do that and create a plugin. The current API works well enough for our needs and there are plenty of alternatives like underscore or lodash if ours isn't good enough. Besides the size increase of adding the code itself, there's the cost and complexity of documentation and maintenance so it's not something I think we want to add.

Changed September 19, 2012 09:52PM UTC by anonymous comment:6

I understand thanks to the both of you for a quick response. Cheers!