【问题标题】:How does one provide default parameters in Javascript anonymous functions?如何在 Javascript 匿名函数中提供默认参数?
【发布时间】:2016-03-30 14:04:59
【问题描述】:

我是 JS 新手,我需要使用匿名函数,但是当我为命名函数中的参数提供默认值时,我收到错误“Uncaught SyntaxError: Unexpected token =".

以下是代码摘录:

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

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

【问题讨论】:

标签: javascript web closures anonymous-function default-parameters


【解决方案1】:

并非所有browsers support 都使用这种语法,因此您需要按照老派的方式进行操作

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

【讨论】:

    【解决方案2】:

    或 Javascript 快捷方式

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

    【讨论】:

      猜你喜欢
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多