目前,我的情况如下:
let x = 0
[x, error] = divide(1, x)
但是代码是错误的,因为error
尚未声明变量。
如果我将let
关键字放在前面,则该关键字也将无效,因为x
已经声明了。
let x = 0
let [x, error] = divide(1, x) // Error
那么,是否存在让我在元组解构期间声明一些变量的快捷方式或语法糖,例如以下内容?
let x = 0
[x, let error] = divide(1, x)
我不相信有任何这样的捷径。您能做的最好的事情是:
在一行中声明所有尚未声明的变量,然后使用简单的解构。这似乎是最神圣的。
let error, another;
[x, error, another] = divide(1, x);
使用两种解构,一种用于需要声明的变量,另一种用于不需要的变量。但是,当您拿走对象的其余部分时,这将无法很好地工作。
let [, error, another] = [x] = divide(1, x);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句