0

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?

2
  • 1
    Can config be anything? Semantically, it looks like it will either be undefined or an object (since you're assigning properties it must be in fact). Is there a reason why you don't use ||? Commented Sep 6, 2011 at 18:06
  • 3
    be carefull, typeof null == "object" as well. Commented Sep 6, 2011 at 18:10

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

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

2 Comments

For the record, it would throw an error in case config is undefined.
This code doesn't do anything, except for throw an error when config is undefined (as pimvdb pointed out). No assignments are ever made.
0

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);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.