TypeScript的命名空间
在代码量较大的情况下,为了避免各种变量命名相互冲突,可以将相似功能的函数、类、接口等放置到命名空间内。
同java的包、.net的命名空间一样,Typescript的命名空间可以将代码包裹起来,只对外暴露需要在外部访问的对象。命名空间内的对象通过export
命名空间和模块的区别:
命名空间:内部模块,主要用户组织代码,避免命名冲突。
模块:ts的外部模块的简称,侧重代码的复用,一个模块里可能会有多个命名空间。
命名空间定义
使用namespace将类和方法封装成私有的,外部想要调用的类通过export暴露出来;
不同命名空间内的类可以重名,不会导致冲突;
外部调用时需要加上namespace;
namespace A {interface Animal {name:string;eat():void;}// 命名空间内的类和方法是私有的,外部用的话需要加export暴露出来export class Dog implements Animal {name:string;constructor(name:string) {this.name=name;}eat():void {console.log(`${this.name} 吃狗粮.`);}}export class Cat implements Animal {name:string;constructor(name:string) {this.name = name;}eat():void {console.log(`${this.name} 吃猫粮.`);}}}namespace B {interface Animal {name:string;eat():void;}// 命名空间内的类和方法是私有的,外部用的话需要加export暴露出来export class Dog implements Animal {name:string;constructor(name:string) {this.name=name;}eat():void {console.log(`${this.name} 吃狗粮.`);}}export class Cat implements Animal {name:string;constructor(name:string) {this.name = name;}eat():void {console.log(`${this.name} 吃猫粮.`);}}}// 外部通过 命名空间.暴露的类 进行调用let a = new A.Dog('阿黄');a.eat();let b = new B.Cat('小花');b.eat();
命名空间模块化
编写命名空间的模块:./modules/animal.ts
// 使用export,将模块内的命名空间暴露出来export namespace A {interface Animal {name:string;eat():void;}export class Dog implements Animal {name:string;constructor(name:string) {this.name=name;}eat():void {console.log(`${this.name} 吃狗粮.`);}}}export namespace B {interface Animal {name:string;eat():void;}export class Dog implements Animal {name:string;constructor(name:string) {this.name=name;}eat():void {console.log(`${this.name} 吃狗粮.`);}}}
- 编写引入ts ```typescript import {A,B} from ‘./modules/animal’
// 外部通过 命名空间.暴露的类 进行调用 let a = new A.Dog(‘阿黄’); a.eat();
let b = new B.Cat(‘小花’); b.eat();
<a name="249fa55f"></a>## 命名空间分离到多文件将Validation命名空间分割成多个文件,尽管是不同的文件,它们仍是同一个命名空间,并且在使用的时候就如同它们在一个文件中定义一样。因为不同文件之间存在依赖关系,所以我们加入了引用标签来告诉编译器文件之间的关联。Validation.js```typescriptnamespace Validation {export interface StringValidator {isAcceptable(s: string): boolean;}}
LettersOnlyValidator.ts
/// <reference path="Validation.ts" />namespace Validation {const lettersRegexp = /^[A-Za-z]+$/;export class LettersOnlyValidator implements StringValidator {isAcceptable(s: string) {return lettersRegexp.test(s);}}}
ZipCodeValidator.ts
/// <reference path="Validation.ts" />namespace Validation {const numberRegexp = /^[0-9]+$/;export class ZipCodeValidator implements StringValidator {isAcceptable(s: string) {return s.length === 5 && numberRegexp.test(s);}}}
测试:
/// <reference path="Validation.ts" />/// <reference path="LettersOnlyValidator.ts" />/// <reference path="ZipCodeValidator.ts" />// Some samples to trylet strings = ["Hello", "98052", "101"];// Validators to uselet validators: { [s: string]: Validation.StringValidator; } = {};validators["ZIP code"] = new Validation.ZipCodeValidator();validators["Letters only"] = new Validation.LettersOnlyValidator();// Show whether each string passed each validatorfor (let s of strings) {for (let name in validators) {console.log(`"${ s }" - ${ validators[name].isAcceptable(s) ? "matches" : "does not match" } ${ name }`);}}
别名
使用import q = x.y.z给对象起一个短名字
namespace Shapes {export namespace Polygons {export class Triangle { }export class Square { }}}import polygons = Shapes.Polygons;let sq = new polygons.Square(); // Same as "new Shapes.Polygons.Square()"
