TypeScript 1.5 模块系统 - 导出为单例
TypeScript 1.5 module system - export as singleton
考虑以下代码:
class Person {
constructor(public name:string) {
}
}
let person = new Person("Dieter");
export { person };
如果我在多个文件中导入这个人对象,我确定我总是得到相同的对象吗?
我只需要一个实例,我使用类的原因是为了更好的可读性。
仅供参考:我使用 webpack 将所有这些打字稿模块打包到一个文件中。
If I import this person object in multiple files, am I sure that I always get the same object?
是的。这是一个常见的模式
考虑以下代码:
class Person {
constructor(public name:string) {
}
}
let person = new Person("Dieter");
export { person };
如果我在多个文件中导入这个人对象,我确定我总是得到相同的对象吗?
我只需要一个实例,我使用类的原因是为了更好的可读性。
仅供参考:我使用 webpack 将所有这些打字稿模块打包到一个文件中。
If I import this person object in multiple files, am I sure that I always get the same object?
是的。这是一个常见的模式