TypeScript 命名空间
命名空间是一种逻辑上组织相关代码的方式。这是 TypeScript 内置的,与 JavaScript 不同,在 JavaScript 中,变量的声明会进入全局作用域中,如果在同一个项目中使用多个 JavaScript 文件,则存在可能会覆盖或误解相同变量的情况,这会导致JavaScript中的“全局命名空间污染问题”。
定义一个命名空间
命名空间定义以关键字 namespace 开始,后跟命名空间的名称,如下所示 –
namespace SomeNameSpaceName {
export interface ISomeInterfaceName { }
export class SomeClassName { }
}
应该通过关键字 export 标记外部命名空间中应访问的类或接口。
要在另一个命名空间中访问类或接口,语法将是namespaceName.className。
SomeNameSpaceName.SomeClassName;
如果第一个命名空间在单独的TypeScript文件中,则应使用三斜线引用语法引用它。
/// <reference path = "SomeFileName.ts" />
以下程序示例了对命名空间的使用:
FileName :IShape.ts
----------
namespace Drawing {
export interface IShape {
draw();
}
}
FileName :Circle.ts
----------
/// <reference path = "IShape.ts" />
namespace Drawing {
export class Circle implements IShape {
public draw() {
console.log("Circle is drawn");
}
FileName :Triangle.ts
----------
/// <reference path = "IShape.ts" />
namespace Drawing {
export class Triangle implements IShape {
public draw() {
console.log("Triangle is drawn");
}
}
FileName : TestShape.ts
/// <reference path = "IShape.ts" />
/// <reference path = "Circle.ts" />
/// <reference path = "Triangle.ts" />
function drawAllShapes(shape:Drawing.IShape) {
shape.draw();
}
drawAllShapes(new Drawing.Circle());
drawAllShapes(new Drawing.Triangle());
}
}
}
上述代码可以使用以下命令进行编译和执行:
tsc --out app.js TestShape.ts
node app.js
在编译时,将生成以下JavaScript代码(app.js)。
//Generated by typescript 1.8.10
/// <reference path = "IShape.ts" />
var Drawing;
(function (Drawing) {
var Circle = (function () {
function Circle() {
}
Circle.prototype.draw = function () {
console.log("Cirlce is drawn");
};
return Circle;
}());
Drawing.Circle = Circle;
})(Drawing || (Drawing = {}));
/// <reference path = "IShape.ts" />
var Drawing;
(function (Drawing) {
var Triangle = (function () {
function Triangle() {
}
Triangle.prototype.draw = function () {
console.log("Triangle is drawn");
};
return Triangle;
}());
Drawing.Triangle = Triangle;
})(Drawing || (Drawing = {}));
/// <reference path = "IShape.ts" />
/// <reference path = "Circle.ts" />
/// <reference path = "Triangle.ts" />
function drawAllShapes(shape) {
shape.draw();
}
drawAllShapes(new Drawing.Circle());
drawAllShapes(new Drawing.Triangle());
当上述代码被编译和执行时,会产生以下结果 −
Circle is drawn
Triangle is drawn
嵌套命名空间
您可以按以下方式在另一个命名空间内定义一个命名空间 –
namespace namespace_name1 {
export namespace namespace_name2 {
export class class_name { }
}
}
您可以使用点 (.) 运算符来访问嵌套命名空间的成员,如下所示−
FileName : Invoice.ts
namespace tutorialPoint {
export namespace invoiceApp {
export class Invoice {
public calculateDiscount(price: number) {
return price * .40;
}
}
}
}
FileName: InvoiceTest.ts
/// <reference path = "Invoice.ts" />
var invoice = new tutorialPoint.invoiceApp.Invoice();
console.log(invoice.calculateDiscount(500));
以上代码可使用以下命令进行编译和执行:
tsc --out app.js InvoiceTest.ts
node app.js
编译时,将会生成以下的JavaScript代码(app.js)。
//Generated by typescript 1.8.10
var tutorialPoint;
(function (tutorialPoint) {
var invoiceApp;
(function (invoiceApp) {
var Invoice = (function () {
function Invoice() {
}
Invoice.prototype.calculateDiscount = function (price) {
return price * .40;
};
return Invoice;
}());
invoiceApp.Invoice = Invoice;
})(invoiceApp = tutorialPoint.invoiceApp || (tutorialPoint.invoiceApp = {}));
})(tutorialPoint || (tutorialPoint = {}));
/// <reference path = "Invoice.ts" />
var invoice = new tutorialPoint.invoiceApp.Invoice();
console.log(invoice.calculateDiscount(500));
当上述代码被编译和执行时,会产生以下结果−
200