我只是找不到对声明的ES6类的引用存储在哪里,我本希望它们出现在window对象中,但它们并没有出现在那里。
我不认为这是ES6 classes : what about instrospection?的副本,因为他要求的是类的存在检查,我想要的是可用类的列表。
例如:
class Test {
constructor() {
}
}
window.Test // undefined我想要的是扩展我的类的所有类的列表
为了澄清一下,我有一个类似于下面这样的结构:
class Base {
constructor(components) {
for(let component of components) {
window[component](); // window.Test2 not defined
}
}
start() {
new this();
}
}
class Test2 extends Base {
constructor() {
super();
}
}
class Test extends Base {
constructor() {
super(['Test2','Test2']);
}
}
Test.start();这只是我的结构的一个抽象,简而言之,我必须在super(['Test2', 'Test2'])中使用字符串
目前,我正在做这样的事情
Base.register(Test2);每一节课,我都想摆脱它。
发布于 2016-04-06 01:22:42
您可以使用Class expressions将它们存储在某种类型的数组中,但如果我是您,我可能不会这么做。
var Test = class Test {
constructor() {
}
}
allClasses.push(Test);发布于 2016-04-06 01:25:54
JavaScript6中引入了JavaScript类,它们是ECMAScript现有的基于原型的继承之上的语法糖。类语法并没有为JavaScript引入新的面向对象的继承模型。JavaScript类提供了更简单、更清晰的语法来创建对象和处理继承。
基本上,ES6类被编译成普通的老式Javascript函数。您可以将它们“存储”在window对象中,但这是一个主要的陷阱,因为您正在扼杀ES6引入的整个模块模式。
发布于 2016-04-06 01:26:36
如果你想要一个类的“模块”,理论上你可以这样做:
// some-class.js
export default class SomeClass {}然后:
// another-class.js
export default class AnotherClass {}和你的入口文件:
// index.js
import SomeClass from './some-class.js' // extensions optional; here just for clarity
import AnotherClass from './another-class.js'
export default {
SomeClass,
AnotherClass
}如果您将所有这些都嵌入到同一目录中(我们将该目录称为example),那么您可以在需要的任何地方导入整个模块:
// something-that-requires-your-module.js
// this will by default enter through the `index.js` file of your `example` directory
import Example from './example';
console.log(Example.SomeClass);
console.log(Example.AnotherClass);https://stackoverflow.com/questions/36432992
复制相似问题