Alex
Alex

Reputation: 5724

Javascript object defaulting

I want a code-golf solution for something that does the equivalent of the following:

myFunc = function( config ){
    if ( typeof config !== 'object' ) { config = {}; }

    config.property = config.property || 123;
};

Basically, is there a shorter way to ensure I am always passing an object, create it if not, and assign random values to it?

Upvotes: 0

Views: 679

Answers (4)

jfriend00
jfriend00

Reputation: 707376

Since you want to protect against config being passed as something other than a normal object, I don't think you can shorten it or use most of the other versions offered here. In fact, I think you have to add the extra test to see if config is not null because null has typeof 'object'.

myFunc = function( config ){
    if ( !config || typeof config !== 'object' ) { config = {}; }

    config.property = config.property || 123;
};

Test cases that this works for:

myFunc(null);
myFunc();
myFunc("{}");
myFunc("{property: 100}");
myFunc("aa");
myFunc([]);
myFunc(5);

Upvotes: 0

J. Holmes
J. Holmes

Reputation: 18546

I usually use a merge helper, usually jQuery.Extend() (see documentation). But if you didn't want to use jQuery, a similar helper would be trivial to write. But I would normally do something like:

myFunc = function( config ){
    var defaults = { property: 123 };
    config = $.extend({}, defaults, config);
};

Basically, you are extending an empty object with the defaults, then the config you pass in. You will always get a new object that has at least your defaults properties. I would definately recommend abstracting your object merging code into its own helper, if only so you don't repeat the same logic in every constructor.

Upvotes: 1

jmar777
jmar777

Reputation: 39649

Here's another alternative that is a little more efficient in defaulting the values. In the current code (with the || short-circuiting on the right-side of the assignment), assignments are always made, even if a value is already set. Using the syntax below, assignments are only made when the default value is needed.

var myFunc = function(config) {
    config || (config = {});
    config.property || (config.property = 123);
};

Please note that this is a micro-optimization and will never make a measurable performance difference, but I think it's a neat approach and the question itself is kind of micro-optimizey to start with :)


Edit: this is probably assumed, but perhaps worth saying anyway. Just make sure that your "is checked?" expression on the left-hand side of the || makes sense for the type of values being set. E.g., if 0 is a valid value, then you would want a more robust expression than what's currently there (which will assign the default on any falsey value). An alternative would be to check if the property is defined.

Upvotes: 1

Mike Robinson
Mike Robinson

Reputation: 25159

One liner, although variable names are kept longer for readability:

myFunc = function(config) {
 (config.property) ? config : {property: 123};
}

Upvotes: 0

Related Questions