javascript - Declaring variables in for loop using object properties -


i trying find shorter way of declearing variables using properties of object.

i have "useroptions" object passed in parameter this:

var example = new myfunction({     maxlimit: 30, }); 

in myfunction have object called "defaultoptions" compare useroptions declare options variables. following example may seem short enough. starts inefficient when number of options increases.

function myfunction(useroptions) {     var defaultoptions = {         maxlimit: 20,         minlimit: 10     }      // apply user options     (var in defaultoptions) {         if (useroptions.hasownproperty(i)) {             defaultoptions[i] = useroptions[i];         }     }      // declare variables     var maxlimit = defaultoptions.maxlimit;     var minlimit = defaultoptions.minlimit; } 

this question little bit similar trying do. answers suggest accessing object properties 1 one did.

is there better way of doing task? declaring variables in loop or extracting properties of updated options object variables?

seems looking object.assign, arguments on right overwrite overlapping properties of objects left:

console.log(object.assign({ id: 1, name: 'hello' }, { name: 'world' }));

which used like:

var config = object.assign(defaultoptions, usersuppliedoptions); 

if instead wanting regular variables , have access es6 use destructuring assignment make things more concise:

let { minlimit, maxlimit } = defaultoptions; 

Comments