zaf
zaf

Reputation: 23244

Recommended way to handle required and optional function parameters

I have a javascript function that takes two parameters, 'key' and 'value'.

If only one parameter is given then the 'key' parameter is given a default value and the undefined 'value' parameter gets the 'key' parameter value.

function thingSet(key,value){
    if(typeof value === 'undefined'){
        value=key;
        key='_default';
    }
    //... use key and value
}

The code works but I feel abit uneasy for some reason.

Are there better ways to do this?

Upvotes: 1

Views: 231

Answers (6)

georg
georg

Reputation: 214949

It's hard to discuss design questions on a dummy example, but I'd prefer a function that always accepts one parameter, which can be a simple value, or a hash of values. Consider this slightly more realistic example:

function setName(opt) {
    if (typeof opt != "object") {
        var p = opt.split(" ");
        opt = { first: p[0], last: p[1] };
    }
    $.extend(this, opt);
}

This can be used as person.setName('John Doe') or person.setName({last:'Doe'})

Upvotes: 0

KooiInc
KooiInc

Reputation: 122906

You can set default values like this:

function thingSet(key,value){
    key = key || '_default';
    value = value || key;
    //... use key and value
}

At least, that is what I make of your function. The unease may be due to the fact that in your function key may be undefined too, in which case the assignment after checking the condition if(typeof value === 'undefined') still may result in an undefined value

You can check for existence of at least one parameter using arguments.length.

function thingSet(key,value){
    if (!arguments.length) {
       alert('please supply at least one parameter'); 
       return true;
    }
    key = key || '_default';
    value = value || key;
    //... use key and value
}

Upvotes: 1

Evert
Evert

Reputation: 8541

I normally do this with JSON

myfunction = function(args){
    args.key =  (typof(args.key) == "undefined")?args.key = "_default":args.key;
    args.value =  (typof(args.value) == "undefined")?args.key:args.value;
}

myfunction({key:"something",value:"something else"})

that way you know which variable you are passing to the function and don't have to assume anything from within the function.

Upvotes: 0

Tomas Vana
Tomas Vana

Reputation: 18775

This is pretty standard and heavily used "overloading" mechanism in javascript. You'll find it all over the libraries like jQuery.

Like many dynamic language constructs, there is a gap between what compiler can check for you and what you have to keep as a convention, perhaps documenting it thouroughly.

The power comes at a price of robustness. If you use this kind of tricks, you have to make sure everybody understands the implied API and uses it accordingly.

Upvotes: 2

aaroncatlin
aaroncatlin

Reputation: 3271

Seems fine to me. The only thing I'd have done differently would be a more direct comparison on value:

if(value == undefined){

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382696

You can refactor it like this:

function thingSet (key, value) {
    key = key || '_default';
    value = value || key;
    //... use key and value
}

That's nice short-circuit evaluation allowing you to set default values easily there.

Upvotes: 2

Related Questions