2

We are using a widget that takes object as parameter. I want some king of object inheritance to customize widget. Default setting should be default then inherit object (add and update properties) which should give desired object as shown in code below.

defaultSettings = {

     setting1 = "setting1 value"; 
     setting2 = "setting2 value"; 

}

customSettings  = {

     setting2 = "setting2 value - custom";  //Overwrite defaultSettings 2    
     setting3 = "setting3 value";  //New Setting 

}

finalObject = {

    setting1 = "setting2 value - custom"; 
    setting2 = "setting2 value"; 
    setting3 = "setting2 value"; 
} 

I have googled before asking this but not sure that is most efficient techniques to achieve this?

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
fe-ninja
  • 163
  • 1
  • 8

3 Answers3

2

Inheritance wouldn't usually be the way to do what you're describing. Instead, what you usually do is have an object with defaults:

var defaults = {
   setting1: "default1",
   setting2: "default2",
   setting3: "default3"
};

...and then have the function accept an object which may have any of those properties (or not have them), and then build a combined result, like this:

function apiCall(o) {
    var options = {};
    var key;

    for (key in defaults) {
        options[key] = defaults[key];
    }
    for (key in o) {
        options[key] = o[key];
    }

    // ...then use `options` here...
}

This is such a common pattern that a number of libraries have functions for it, such as jQuery's extend, Prototype's extend, etc.

A general-purpose extend accepting an arbitrary number of objects (each object having precedence over the previous one) looks like this:

function extend() {
    var rv = {},
        key,
        source,
        index;

    for (index = 0; index < arguments.length; ++index) {
        source = arguments[index];
        for (key in source) {
            rv[key] = source[key];
        }
    }

    return rv;
}

If you had an extend like that, then the API call function looks like this instead:

function apiCall(options) {
    options = extend(defaults, options);

    // ...
}

But if you are interested in inheritance in JavaScript, I created a helper library called Lineage to make it easier, and I write about JavaScript (including inheritance) on anemic little my blog periodically.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

If you're using Node.js you can use util.inherits(constructor, superConstructor). If you aren't, here is some library-dependent documentation:

Implementations may differ (e.g. deep copy), so I'ld recommend you to test it thoroughly before marking it ready for production.

cutsoy
  • 10,127
  • 4
  • 40
  • 57
  • Please note that this only works after you converted your settings-hashes into Object Oriented code first (manually). – cutsoy May 24 '13 at 13:00
0

You can set default values ​​in the class, and if you pass values​​, the class assumes these values.

var foo = function (settings){
    this.Settings = settings || {};

    this.Settings.a = this.Settings.a  || 'Default';
    this.Settings.b = this.Settings.b  || 'Default'
    console.log(this.Settings)
}

//default
var test = new foo(); //Object {a: "default", b: "default"}

//custom
var settings = {
    a:'a', 
    b:'b'
};
var x = new foo(settings); // Object {a: "a", b: "b"}


//add new settings
x.Settings.c = 'test';

console.log(x.Settings); // Object {a: "a", b: "b", c: "test"}