1

I'd like to configure a Javascript object using a syntax something like JQuery .ajax(..) one and not with a list of parameters, only defining values different from defaults.
So far I wrote it like:

function ObjFn(config) { var var1 = 200 var var2 = "qwerty" var var3 = 555 if (config instanceof Object){ console.log("-> config") if (config.var1 !== undefined) var1 = config.var1 if (config.var2 !== undefined) var2 = config.var2 if (config.var3 !== undefined) var3 = config.var3 } this.describe = function(){ console.log("var1: "+var1+" - "+"var2: "+var2+" - "+"var3: "+var3+" - "+"config: "+config+"\n") } } 

But I'm wondering if it is possible to avoid writing a list of conditionals:

if (confing.var !== undefined) var = config.var 

For each possible overwritable variable but simply using in some way the name of the variable.

4 Answers 4

6

You could destructure config into the var1, var2, and var3 variables, using default property values. If config isn't an object, destructure from an empty object instead so that those default parameters will be used:

function ObjFn(config) { const { var1=200, var2="querty", var3=555} = config instanceof Object ? config : {}; this.describe = function(){ console.log("var1: "+var1+" - "+"var2: "+var2+" - "+"var3: "+var3+" - "+"config: "+config+"\n") } } const instance = new ObjFn({ var1: 333 }); instance.describe(); const instance2 = new ObjFn(); instance2.describe();

If the parameter, if passed, will always be an object, you can destructure in the parameter list instead:

function ObjFn({ var1=200, var2="querty", var3=555} = {}) { this.describe = function(){ console.log("var1: "+var1+" - "+"var2: "+var2+" - "+"var3: "+var3) } } const instance = new ObjFn({ var1: 333 }); instance.describe(); const instance2 = new ObjFn(); instance2.describe();

Sign up to request clarification or add additional context in comments.

Comments

0

This should work. Simple and clean:

function ObjFn(config) { var var1 = config.var1 || 200 var var2 = config.var2 || "qwerty" var var3 = config.var3 || 555 this.describe = function(){ console.log("var1: "+var1+" - "+"var2: "+var2+" - "+"var3: "+var3+" - "+"config: "+config+"\n") } } 

Comments

0

Use an object to hold your variables and use Object.assign() to merge the config object to your default values, however this could add new properties to your vars object:

function ObjFn(config) { const vars = Object.assign({ var1: 200, var2: 'qwerty', var3: 555 }, config); this.describe = function() { const str = Object.entries(vars).map(([k, v]) => `${k}: ${v}`).join(' - '); console.log(str); } } new ObjFn({ var2: 'azerty', newProp: 'hello' }).describe(); new ObjFn({ var1: 1, var3: 0 }).describe(); new ObjFn().describe();

If you do not want to add new properties, iterate over the properties of your vars object and set them from the config if the entry exists:

function ObjFn(config) { const vars = { var1: 200, var2: 'qwerty', var3: 555 }; if (config instanceof Object) { for (let prop in vars) { if (config.hasOwnProperty(prop)) { vars[prop] = config[prop]; } } } this.describe = function() { const str = Object.entries(vars).map(([k, v]) => `${k}: ${v}`).join(' - '); console.log(str); } } new ObjFn({ var2: 'azerty', newProp: 'hello' }).describe(); new ObjFn({ var1: 1, var3: 0 }).describe(); new ObjFn().describe();

Comments

0

I'd use an extend function which takes an object as its first argument, then adds properties from all other passed objects which are not present in the first object. This is how a lot of jQuery's methods work under the hood (using jQuery.extend):

function extend(obj) { for (var i = 1; i < arguments.length; i++) { for (var key in arguments[i]) { if (arguments[i].hasOwnProperty(key)) { obj[key] = arguments[i][key]; } } } return obj; } function ObjFn(config) { var params = { var1: 200, var2: 'qwerty', var3: 555 }; if (config instanceof Object) { console.log("-> config"); extend(params, config); } this.describe = function () { console.log("var1: " + params.var1 + " - " + "var2: " + params.var2 + " - " + "var3: " + params.var3 + " - " + "config: " + config + "\n"); } } var instance = new ObjFn({var1: 999}); instance.describe(); var instance2 = new ObjFn(); instance2.describe();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.