es6 在 IF 中声明一个变量(如 es5 )
es6 declare a variable inside an IF ( like es5 )
好的,这已经到了我很恼火的地步,无法解决这个问题。
在 ES5 中,我经常会做类似...
if( (users = resp.results) && users.length > 0 ) {
// do something with users
}
暂时,让我们忘记范围和含义以及所有这些......
从 ES6 开始,Babel 不再喜欢这种语法,Chrome 控制台也不喜欢。
现在 ES6 中有等效的语法吗?
如果你想让它在使用Babel强制执行的'use strict'
时有效,你需要先声明users
。如果您不使用 Babel 或 'use strict',您的原始代码也可以正常工作。
var users;
if( (users = resp.results) && users.length > 0 ) {
// do something with users
}
将与 'use strict'
一起工作。
如果您想继续使用这个技巧,您也可以从转换后的 babel 代码的顶部手动删除 'use strict'
。
好的,这已经到了我很恼火的地步,无法解决这个问题。
在 ES5 中,我经常会做类似...
if( (users = resp.results) && users.length > 0 ) {
// do something with users
}
暂时,让我们忘记范围和含义以及所有这些......
从 ES6 开始,Babel 不再喜欢这种语法,Chrome 控制台也不喜欢。
现在 ES6 中有等效的语法吗?
如果你想让它在使用Babel强制执行的'use strict'
时有效,你需要先声明users
。如果您不使用 Babel 或 'use strict',您的原始代码也可以正常工作。
var users;
if( (users = resp.results) && users.length > 0 ) {
// do something with users
}
将与 'use strict'
一起工作。
如果您想继续使用这个技巧,您也可以从转换后的 babel 代码的顶部手动删除 'use strict'
。