Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#12573 closed feature (plugin)

Better API for Extend

Reported by: adkison.adrian@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.8.0
Keywords: Cc:
Blocked by: Blocking:

Description

$.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.

Change History (6)

comment:1 Changed 6 years ago by Rick Waldron

Resolution: wontfix
Status: newclosed

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

comment:2 Changed 6 years ago by adkison.adrian@…

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

comment:3 Changed 6 years ago by anonymous

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

comment:4 Changed 6 years ago by dmethvin

Resolution: wontfix
Status: closedreopened

comment:5 Changed 6 years ago by dmethvin

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.

comment:6 Changed 6 years ago by anonymous

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

Note: See TracTickets for help on using tickets.