【发布时间】:2017-01-09 16:47:23
【问题描述】:
Babel 没有转译 ES6 计算属性名称。它会编译其他所有内容。不知道发生了什么。使用最新版本的 cli。
var name = "John";
var age = 12;
var count = 5;
var postfix = 'age';
var person = {
name,
age,
count,
postfix,
printName(){
console.log(this.name);
this.count--;
while(this.count){
this.printName();
}
},
['print' + this.postfix]: function(){
console.log(this.age);
}
};
person.printName();
person.printAge();
Babel 输出
var name = "John";
var age = 12;
var count = 5;
var postfix = 'age';
var person = _defineProperty({
name: name,
age: age,
count: count,
postfix: postfix,
printName: function printName() {
console.log(this.name);
this.count--;
while (this.count) {
this.printName();
}
}
}, 'print' + postfix, function () { // Look here
console.log(this.age);
});
【问题讨论】:
-
看起来它被正确转译了,结果函数名应该是
printage(),但你正试图使用printAge()来访问它,也许这就是问题所在。 -
您的代码示例也不匹配。第一个使用
this.postfix,第二个使用postfix作为属性名称。我会第二个@Stubb0rn,你需要person.printage(); -
似乎是我的错误+一些混乱。我认为它将被转换为计算值而不是“打印”+后缀。
-
它被转换为计算值
'print' + postfix. -
是的(因此我评论中的混淆部分)。我实际上认为它会被计算为 printAge。
标签: javascript ecmascript-6 babeljs babel-cli