opes
opes

Reputation: 1828

Object Specifiers in Javascript

In Douglas Crockford's book "Javascript: The Good Parts", he mentions Object Specifiers in passing values to a new object. Essentially, instead of passing parameters to a function in a certain order, he suggests passing an object with the parameters contained within, like so:

var myObject = someFunction({a: 1, b: 2, c: 3});

What isn't explained, however, is how to handle those parameters after they've been passed through. Instead of doing the following to set up default values:

function someFunction(params){
    this.a = params.a || 0; 
    this.b = params.b || 0;
    ...
}

What's another way to handle a large amount of parameters without being as verbose?


EDIT: Looking at the answers below, the use of the for-in loop is a great option. What's another way to do it while setting different default values for each property? Is there a way to create an object with various default values and compare against that?

Upvotes: 14

Views: 2150

Answers (4)

Yoshua Elmaryono
Yoshua Elmaryono

Reputation: 1

Extending answer by Bergi: var params = $.extend({}, defaults, custom);

We can now use Object.assign({}, defaults, custom) in ES6 without any library.

Upvotes: 0

Bergi
Bergi

Reputation: 665536

Usually it is done using a defaults object, from which you copy properties if they are not existing in the params. That object might be private or available to the public for configuration.

var defaults = { a:null, b:1, ...};
function someFunction(params) {
    for (var prop in defaults)
        if (prop in params)
            this[prop] = params[prop];
        else
            this[prop] = defaults[prop];
}

or

function someFunction(custom) {
    var params = { a:null, b:1, ...};
    for (var prop in custom)
        params[prop] = custom[prop];
    // now use params
}

Sometimes also the custom/params objects which is passed into the function is extended itself, but then in the docs it should be mentioned explicitly that the object can get modified:

var defaults = { a:null, b:1, ...};
function someFunction(params) {
    // modifies params object!
    for (var prop in defaults)
        if (! (prop in params))
            params[prop] = defaults[prop];
    // now use params
}

If you are using a library with an extend function, you often can shorten this initialisation to

    var params = $.extend({}, defaults, custom);
    // create an empty object, copy the defaults, and overwrite with custom properties

Upvotes: 9

Sushanth --
Sushanth --

Reputation: 55750

How about using a for-in loop

function someFunction(params){

    for(var key in params){
       if( params.hasOwnProperty(key){
           var def = null;
           if(key == 'a' || key == 'b'){
              def = 10;
           }
           if(key == 'c' || key == 'd'){
              def = undefined;
           }
           this[key] = params[key]  || def ;
       }
    }
}

Upvotes: 4

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

Loop through the fields in the object passed as the parameter, creating each field as a field on the this object.

function someFunction(params){
   for(x in params){
     this[x] = params[x];
   }
}

Working Example: http://jsfiddle.net/59kzD/

Upvotes: 1

Related Questions