Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 639
opes Avatar asked Nov 12 '12 09:11

opes


People also ask

What are object properties in JavaScript?

An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects.

What are objects in JavaScript?

In JavaScript, an object is an unordered collection of key-value pairs. Each key-value pair is called a property. The key of a property can be a string. And the value of a property can be any value, e.g., a string, a number, an array, and even a function.

How many types of objects are there in JavaScript?

There are two types of object properties: The data property and the accessor property. Each property has corresponding attributes. Each attribute is accessed internally by the JavaScript engine, but you can set them through Object.


3 Answers

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
like image 98
Bergi Avatar answered Oct 14 '22 01:10

Bergi


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 ;
       }
    }
}
like image 4
Sushanth -- Avatar answered Oct 14 '22 03:10

Sushanth --


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/

like image 1
Kevin Bowersox Avatar answered Oct 14 '22 01:10

Kevin Bowersox