【问题标题】:Blank identifier in JavascriptJavascript中的空白标识符
【发布时间】:2020-04-13 12:09:52
【问题描述】:

在 golang 中有 _(空白标识符)。

myValue, _, _ := myFunction()

这样你可以省略函数的第二个和第三个返回值。

在javascript中可以做到这一点吗?

function myFunction() {
   return [1,2,3]
}

// Something like this
const [first, _, _] = myFunction()

【问题讨论】:

    标签: javascript arrays typescript javascript-objects


    【解决方案1】:

    解构时,可以完全删除未使用的项(无需指定以后不会使用的变量名),并且未使用的尾随数组项甚至不需要逗号(具有数组]在你需要的最后一个解构项结束):

    function myFunction() {
       return [1,2,3]
    }
    
    const [first] = myFunction()
    const [, second] = myFunction()
    const [,, third] = myFunction()
    
    console.log(first, second, third);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 2023-03-10
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多