TypeScript 1.5 模块系统 - 导出为单例

标签 typescript typescript1.5

考虑以下代码:

class Person {
    constructor(public name:string) {

    }
}

let person = new Person("Dieter");

export { person };

如果我在多个文件中导入此 person 对象,我确定总是得到相同的对象吗?

我只需要一个实例,我使用类的原因是为了更好的可读性。

仅供引用:我使用 webpack 将所有这些 typescript 模块捆绑到一个文件中。

最佳答案

If I import this person object in multiple files, am I sure that I always get the same object?

是的。这是一种常见的模式🌹

关于TypeScript 1.5 模块系统 - 导出为单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31670844/

相关文章:

javascript - 在两个子组件之间共享一个方法(Angular)

javascript - TypeScript 中的深度克隆(保留类型)

javascript - TypeScript+SystemJS 使用 JSON 和文本插件

typescript - 执行底层函数时调用 typescript 装饰器方法

typescript - 如何在 TypeScript 中正确更改变量的类型?

typescript - Next.js - 导入 TypeScript 模块时找不到模块

typescript - 从字符串操作推断字符串文字类型

typescript 输出属性错误

typescript - 编译 Angular2 ts 文件

visual-studio-2015 - VS2015 的 Typescript 1.5 无法访问互联网