Object Extension

by Matt Anderson

JavaScript

console.clear();

var Fusion = {};

Fusion.Object = function(toExtend, forceExtend) {
    return this.constructor(toExtend || {}, forceExtend);
};
Fusion.Object.prototype.constructor = function(toExtend, forceExtend){
    return this.extend(toExtend, forceExtend);  
};
Fusion.Object.prototype.extend = function(toExtend, forceExtend){
    Object.keys(toExtend).forEach(function(aKey){
        if(!this._keysToIgnore ||  this._keysToIgnore.indexOf(aKey) == -1 || forceExtend)
            this[aKey] = toExtend[aKey];
    }.bind(this));
    return this;
};


var Campaign = new Fusion.Object({
    _keysToIgnore : [ "id" ]
});

console.log(Campaign);

var NewCampaign = Campaign.extend({
    "id" : "123" 
});

console.log(NewCampaign);