4

I've read some questions here about this:

  1. This is good

    function(a, b) {
        a = (typeof a === 'undefined')? 'default_val' : a;
        b = (typeof b === 'undefined')? 'default_val' : b;
    }
    
  2. This is somewhat troublesome (works for anything but boolean values)

    function(a, b) {
        a = a || 'default_val';
        b = b || 'default_val';
    }
    

    If you pass false it break the logic. So this should be marked as a bad habit

  3. There were a few question, but I have never seen the answer to this (nobody answered directly to the question): is this (last method) good (does anyone sees any issues with it) ?

    function(a = 'default_val', b = 'default_val') {
        // your code
    }
    

I have tested this 3th method but couldn't find any issues. I would like the last method. It is more cleaner and looks more like the structure of other languages.

Thanks

5
  • There IS problem: your third statement is an invalid declaration, and your function is never defined. Commented Oct 16, 2012 at 7:47
  • function(a = 'default_val', b = 'default_val') is Syntax error Commented Oct 16, 2012 at 7:48
  • 1
    Javascript does not allow to specify default value for function parameter. Moreover, syntax 1 also is not fully valid for check because caller can pass undefined value into function. You must check arguments.length to do this work. Commented Oct 16, 2012 at 7:48
  • "is not fully valid for check because caller can pass undefined" I guess so. But this is the best way (not the perfect one). Using arguments.length is not an option when you don't know how many arguments will there be. Commented Oct 16, 2012 at 8:01
  • @Symba: arguments.length will be set to how many arguments were passed in. So in your case if it is 0 both arguments need to be set to default, if it is 1 then the second argument need to be set to default anything more than 1 then don't assign default value. This logic can be applied to any number of arguments. Commented Oct 16, 2012 at 8:21

2 Answers 2

2

In current ECMAScript implementation there is no way to do it like 3. And I really wish this way too. Its proposed in next implementation so called "Harmony" more about it here http://wiki.ecmascript.org/doku.php?id=harmony:parameter_default_values

Also just came in my mind how I'm actually doing it. Usually I pass an arguments as an object for instance,

var options = { x : 20, y : 30 }

then I'm doing in the function

function iAcceptDefaults(options) {
    var defaults = {
        x: 40 
    }
    var newParameters = $.extend({},options, defaults) // here we using jQuery method extend to mix defaults parameters with options

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

3 Comments

So your advice is to use jQuery to get some syntactic sugar for JS? Not very clever IMHO.
I agree with bjornd. Using the jQuery method (or any other library) to solve this is a NO NO
You can easily make it without jQuery mate, its just an example, use google to find out $.extend alternatives
1

The third example is syntactically incorrect in the most of the modern browsers. Currently it would work in FireFox only (here is a fixed issue), since this syntax is a part of not yet approved ECMAScript Harmony standard.

2 Comments

Has anything changed now that its 2013?
@GamesBrainiac Nothing have changed really. Still only FireFox supports this feature (kangax.github.io/es5-compat-table/es6). I think most of the ECMAScript Harmony features will be adopted by browser developers only when the standard will be completed and released. JS engined are much more sophisticated that the CSS ones. So it's much harder to introduce new experimental features without affecting performance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.