Search code examples
javascriptwebclosuresanonymous-functiondefault-parameters

How does one provide default parameters in Javascript anonymous functions?


I am new to JS and I need to use an anonymous function, but when I provide default values to parameters as in named functions, I get the error "Uncaught SyntaxError: Unexpected token =".

Here's the code excerpt:

//some properties
initResize: function(isPlayerInitializing=true){ 
    //some execution
},
//some more properties

I would like to know how one would provide default values to parameters of anonymous functions in Javascript.


Solution

  • Not all browsers support that syntax so you need to do it the old school way

    initResize: function(isPlayerInitializing){ 
        if (isPlayerInitializing===undefined) {
            isPlayerInitializing = true;
        }
        //some execution
    },