如何在Javascript匿名函数中提供默认参数

How does one provide default parameters in Javascript anonymous functions?

本文关键字:默认 参数 函数 Javascript      更新时间:2023-10-01

我是JS的新手,需要使用一个匿名函数,但当我像在命名函数中那样为参数提供默认值时,我会得到错误"Uncaught SyntaxError:Unexpected token="。

以下是代码摘录:

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

我想知道如何在Javascript中为匿名函数的参数提供默认值。

并非所有浏览器都支持该语法,因此您需要使用老式的方法

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

或Javascript快捷方式

initResize: function(isPlayerInitializing){ 
    isPlayerInitializing = isPlayerInitialing || true;
    //some execution
},