官术网_书友最值得收藏!

Namespaces

We can create namespaces in TypeScript using the namespace keyword as follows. All the classes defined under namespace will be scoped under this namespace and will not be attached to the global scope:

namespace Inventory { 
      class Product { 
             constructor (public name: string, public quantity:  
                  number) {   } 
      } 
      // product is accessible 
      var p = new Product('mobile', 101); 
} 
 
// Product class is not accessible outside namespace 
var p = new Inventory.Product('mobile', 101); 

To make the Product class available for access outside the namespace, we need to add an export keyword when defining the Product class, as follows:

module Inventory { 
      export class Product { 
             constructor (public name: string, public quantity: number) {   } 
      } 
} 
 
// Product class is now accessible outside namespace 
var p = new Inventory.Product('mobile', 101); 

We can also share namespaces across files by adding a reference statement at the beginning of the code in the referring files, as follows:

/// <reference path="Inventory.ts" /> 
主站蜘蛛池模板: 郧西县| 邹城市| 韶关市| 呼和浩特市| 柳河县| 麻江县| 通河县| 通山县| 任丘市| 锡林郭勒盟| 招远市| 冷水江市| 濉溪县| 怀宁县| 巴青县| 黎平县| 哈尔滨市| 黎城县| 黄浦区| 商南县| 张家港市| 崇阳县| 潍坊市| 古浪县| 当涂县| 泸水县| 厦门市| 太保市| 客服| 丰原市| 涿鹿县| 利川市| 甘孜| 德钦县| 东宁县| 平塘县| 油尖旺区| 遂溪县| 许昌市| 贺州市| 平罗县|