在代码量较大的情况下,为了避免各种变量命名相冲突,可以将类似功能的函数、类、接口等放置到命名空间中
在TypeScript
中的命名空间中的对象、类、函数等可以通过export
暴露出来通过命名空间名.类名
等来使用
这个暴露是暴露在命名空间外,不是将其在模块中暴露出去
namespace lj3d {
export namespace A {
export class User {
name: String;
constructor(name: String) {
this.name = name;
}
say() {
console.log(`我叫:${this.name}`);
}
}
}
export namespace B {
export class User {
name: String;
constructor(name: String) {
this.name = name;
}
say() {
console.log(`我叫:${this.name}`);
}
}
}
}
var a = new lj3d.A.User("张三");
var b = new lj3d.B.User("李四");
a.say();
b.say();