0

I am trying to find a shorter way of declearing variables using the properties of an object.

I have a "userOptions" object passed in as a parameter like this:

var example = new myFunction({
    maxLimit: 30,
});

In myFunction I have another object called "defaultOptions" which I compare with userOptions to declare the options as variables. Following example may already seem short enough. However it starts to get inefficient when number of options increases.

function myFunction(userOptions) {
    var defaultOptions = {
        maxLimit: 20,
        minLimit: 10
    }

    // Apply user options
    for (var i in defaultOptions) {
        if (userOptions.hasOwnProperty(i)) {
            defaultOptions[i] = userOptions[i];
        }
    }

    // Declare variables
    var maxLimit = defaultOptions.maxLimit;
    var minLimit = defaultOptions.minLimit;
}

This question is a little bit similar to what I am trying to do. However answers suggest accessing the object properties one by one like I already did.

Is there a better way of doing this task? Like declaring the variables in the for loop or extracting the properties of the updated options object as variables?

3
  • 1
    It's a bit unclear what you're trying to accomplish. If you have this defaultOptions object that has the values you want, why not just access them through that object? I.E., wherever you're accessing maxLimit, just write defaultOptions.maxLimit (you don't need to use the bracket syntax). Commented Sep 1, 2016 at 21:48
  • That's what most libraries (e.g. jQuery and jQuery UI) do. Except maybe for some options that are used really frequently for convenience. Commented Sep 1, 2016 at 21:50
  • If you don't want to keep typing a long name like defaultOptions, you can give it a short name like var do = defaultOptions;. Then it's just do.maxLimit. Commented Sep 1, 2016 at 21:51

1 Answer 1

1

Seems like you are looking for Object.assign, arguments on the right overwrite overlapping properties of objects to the left:

console.log(Object.assign({ id: 1, name: 'Hello' }, { name: 'World' }));

Which could be used like:

var config = Object.assign(defaultOptions, userSuppliedOptions);

If instead you are just wanting regular variables and have access to ES6 you could also use destructuring assignment to make things more concise:

let { minLimit, maxLimit } = defaultOptions;
Sign up to request clarification or add additional context in comments.

4 Comments

His question is about assigning to ordinary variables, not how to merge the objects.
@Barmar - Thanks for pointing that out, I see what you mean. I still think this could be a better solution than assigning to ordinary variables though so I'm going to leave it for the time being
What does this do differently from his for loop?
@Barmar I suppose it's speaking to "I am trying to find a shorter way...", though as you pointed out - I am ignoring the 'declare global variables' part. They could do something silly like: Object.assign(window, { foo: 'bar' }) though...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.