使用 Google Closure Compiler 包含 Ecmascript 6 class?
Include an Ecmascript 6 class using Google Closure Compiler?
如何在使用 Google Closure Compiler 时包含 Ecmascript 6 class?
例如,我在 'stuff/dog.js' 中有一个 class:
class dog {
constructor() {
…
}
addLeg() {
this.legs++;
}
}
我想将它包含在 'stuff/pound.js' 中,这样我就可以写:
let rex = new Dog();
这应该如何处理?我不能将 stuff.dog 用作 class 名称,因此将调用传递给 goog.provide() 似乎不是一个选项。
感谢您的帮助!
编辑:使用最新的 (20160517 1.0) 版本的 Closure 编译器,这可以用普通的 Ecmascript 6 处理:
Animal.js:
export default class{
constructor(){
this.legs = [];
}
addLeg(legId){
this.legs.push( legId );
}
}
Dog.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();
使用 Jeremy 和 Chad 的回答(谢谢,伙计们!)我设法使用类似于此的方法导入了我的 class:
'stuff/dog.js':
goog.module('canine');
canine.dog = class {
constructor() {
…
}
addLeg() {
this.legs++;
}
}
'stuff/pound.js':
goog.require('canine');
let rex = new canine.Dog();
对我来说不明显的一件事是名称空间 ('canine') 不需要与 class 名称或 filename/path 有任何关系。
如何在使用 Google Closure Compiler 时包含 Ecmascript 6 class?
例如,我在 'stuff/dog.js' 中有一个 class:
class dog {
constructor() {
…
}
addLeg() {
this.legs++;
}
}
我想将它包含在 'stuff/pound.js' 中,这样我就可以写:
let rex = new Dog();
这应该如何处理?我不能将 stuff.dog 用作 class 名称,因此将调用传递给 goog.provide() 似乎不是一个选项。
感谢您的帮助!
编辑:使用最新的 (20160517 1.0) 版本的 Closure 编译器,这可以用普通的 Ecmascript 6 处理:
Animal.js:
export default class{
constructor(){
this.legs = [];
}
addLeg(legId){
this.legs.push( legId );
}
}
Dog.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();
使用 Jeremy 和 Chad 的回答(谢谢,伙计们!)我设法使用类似于此的方法导入了我的 class:
'stuff/dog.js':
goog.module('canine');
canine.dog = class {
constructor() {
…
}
addLeg() {
this.legs++;
}
}
'stuff/pound.js':
goog.require('canine');
let rex = new canine.Dog();
对我来说不明显的一件事是名称空间 ('canine') 不需要与 class 名称或 filename/path 有任何关系。