mardi 20 octobre 2015

How can I set default values for an object's keys?

A function receives an object o. The function requires it has four keys, a, b, c, d. For each one that is undefined, we want to set a custom default value. One option would be:

function func(o) {
    o.a = o.a || 12;
    o.b = o.b || function() {}
    // and so on
}

But is there a more elegant or idiomatic option?




Aucun commentaire:

Enregistrer un commentaire