gpt4 book ai didi

javascript - 使用 Google Closure Compiler 包含一个 Ecmascript 6 类?

转载 作者:行者123 更新时间:2023-11-30 16:04:18 26 4
gpt4 key购买 nike

如何在使用 Google Closure Compiler 时包含 Ecmascript 6 类?

例如,我在“stuff/dog.js”中有一个类:

class dog {
constructor() {

}
addLeg() {
this.legs++;
}
}

我想将它包含在“stuff/pound.js”中,这样​​我就可以写:

let rex = new Dog();

应该如何处理?我不能将 stuff.dog 用作类名,因此将调用传递给 goog.provide() 似乎不是一种选择。

感谢您的帮助!

编辑:使用最新的 (20160517 1.0) 版本的 Closure Compiler,这可以用纯 Ecmascript 6 处理:

Animal.js:

export default class{
constructor(){
this.legs = [];
}
addLeg(legId){
this.legs.push( legId );
}
}

狗.js:

import Animal from './Animal';

export default class extends Animal {
constructor(){
super();
[1,2,3,4].forEach(leg=>this.addLeg(leg));
console.log( 'Legs: ' + this.legs.toString() );
}
}

虽然出于某种原因它确实给了我一个警告:Closure Compiler warns "Bad type annotation. Unknown type …" when Ecmascript 6 class is extended

最佳答案

ES6 类可以分配给命名空间:

stuff.dog = class { } 
new stuff.dog();

关于javascript - 使用 Google Closure Compiler 包含一个 Ecmascript 6 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37264968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com