如下图所示,包含访问属性的对象的案例1、案例2和案例3都是计算属性名。
案例1在函数参数中创建这样的对象,而案例2在赋值中创建这样的对象。案例3在语句中创建这样的对象。
为什么案例3会产生语法错误?
案例1
$ node -e "var other = 'b'; console.log({a: 1, [other]: 2}[other]);"
2案例2
$ node -e "var other = 'b'; var obj = {a: 1, [other]: 2}[other]; console.log(obj);"
2案例3
$ node -e "var other = 'b'; {a: 1, [other]: 2}[other];"
[eval]:1
var other = 'b'; {a: 1, [other]: 2}[other];
^
SyntaxError: Unexpected token :
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Object.<anonymous> ([eval]-wrapper:6:22)
at Module._compile (module.js:570:32)
at evalScript (bootstrap_node.js:353:27)
at run (bootstrap_node.js:122:11)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:121:9)
at bootstrap_node.js:504:3节点版本
$ node --version
v6.11.2https://stackoverflow.com/questions/46359164
复制相似问题