有没有办法在与分配变量相同的块中返回新变量?
我有此内联if语句,我想立即返回变量,而不必先声明它。当这样做(参见下文)时,一切工作正常,除了linter eslint抱怨x是未使用的变量。
const y = true
let x = ''
return (x = y === true ? 'it is true' : 'it is not true')
我想做类似的事情(见下文),所以我可以删除x的声明,并在分配它的同一块中返回它。这似乎不可能。
const y = true
return (let x = y === true ? 'it is true' : 'it is not true')
如果有这样做的方法(类似于上面的方法),则首选以这种方式构造代码吗?
const y = true
let x = ''
x = y === true ? 'it is true' : 'it is not true'
return x
谢谢!
好吧,x
是一个未使用的变量。声明,初始化,分配一次,但从不读取。
你要么写
const x = y === true ? 'it is true' : 'it is not true'
return x
或者(只是完全删除变量)
return y === true ? 'it is true' : 'it is not true'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句