Javascript Babel不传输ES6计算属性名称
Babel没有传输ES6计算属性名称。不过,它编译了所有其他内容。不知道发生了什么事。使用最新版本的cliJavascript Babel不传输ES6计算属性名称,javascript,ecmascript-6,babeljs,babel-cli,Javascript,Ecmascript 6,Babeljs,Babel Cli,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.
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();
巴别塔输出
在什么意义上它不是传输计算属性名?在我看来,这完全正确
你指的是它在后缀之前掉了这个吗?这是因为它们是相同的——ES6中计算属性名称中的这一点不引用对象,而是对象外部的任何内容。这里是全局对象,前面的var后缀定义了全局对象的一个属性。因此,this.postfix和postfix在该上下文中是同义的。看起来它被正确地传输了,生成的函数名应该是printage,但您试图使用printage访问它,这可能就是问题所在。您的代码示例也不匹配。第一个使用this.postfix,第二个使用postfix作为属性名。不过我会支持@Stubb0rn,你需要person.printage;似乎是我的错误+一些混乱。我以为它会被转换成计算值,而不是“print”+后缀。它被转换成计算值,即“print”+后缀。是的,因此我的评论中出现了混淆部分。我真的以为它会被计算成印刷版。
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);
});