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

标签 javascript web closures anonymous-function default-parameters

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

以下是代码摘录:

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

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

最佳答案

并非全部 browsers support这种语法所以你需要用老派的方式来做

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

关于javascript - 如何在 Javascript 匿名函数中提供默认参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36311236/

相关文章:

javascript - 检查对象数组中是否存在值

css - 全宽背景图像大小

visual-studio - 在 Azure 中使用 Visual Studio Online 加载测试性能计数器

javascript - 事件绑定(bind)是什么意思?

python - 在 Python 中伪造类似 LISP 的闭包?

javascript - 更改下拉菜单文本对齐实现 1.0.0

javascript - 通过 JavaScript 中的字典对象检查单词有效性 - 是否有更好的方法来引用/构造字典?

javascript - 无法使用 Javascript/Jquery 连续保留文本框的 ID

css - 样式表上的 403 错误 - Raspberry Pi Webserver

node.js - 使用 memwatch-node 识别闭包的内存泄漏