代码之家  ›  专栏  ›  技术社区  ›  Thellimist

Javascript中的空白标识符

  •  1
  • Thellimist  · 技术社区  · 5 年前

    在高朗有 _ (空白标识符)。

    myValue, _, _ := myFunction()
    

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

    在javascript中,可以这样做吗?

    function myFunction() {
       return [1,2,3]
    }
    
    // Something like this
    const [first, _, _] = myFunction()
    
    1 回复  |  直到 5 年前
        1
  •  2
  •   CertainPerformance    5 年前

    解构时,可以删除未使用的项 完全地 (无需指定以后不再使用的变量名),并且未使用的尾随数组项甚至不需要逗号(使用数组 ] 结束于您需要的最终解构项):

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