TypeScript 命名空間

2019-02-13 10:18 更新

命名空間是對相關(guān)代碼進(jìn)行邏輯分組的一種方法。這是內(nèi)置于TypeScript中的,與JavaScript不同,其中變量聲明進(jìn)入全局范圍,如果在同一個項目中使用多個JavaScript文件,則可能會覆蓋或誤解相同的變量,這將導(dǎo)致JavaScript中的“全局命名空間污染問題”。

定義命名空間

命名空間的定義以關(guān)鍵字namespace開頭,后跟命名空間名稱,如下所示:

namespace SomeNameSpaceName { 
   export interface ISomeInterfaceName {      }  
   export class SomeClassName {      }  
} 

應(yīng)在命名空間外部訪問的類或接口應(yīng)使用關(guān)鍵字export標(biāo)記。

要訪問另一個名稱空間中的類或接口,語法將為namespaceName.className

SomeNameSpaceName.SomeClassName;

如果第一個命名空間位于單獨的TypeScript文件中,那么就應(yīng)該使用三斜杠引用語法引用它。

/// <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());
      }
   }
}

上面的代碼可以被編譯并使用下面的命令執(zhí)行:

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());

當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:

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));

上面的代碼可以被編譯并使用下面的命令執(zhí)行:

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));

當(dāng)上述代碼被編譯和執(zhí)行時,它產(chǎn)生了以下結(jié)果:

200
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號