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

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" /> 
主站蜘蛛池模板: 成安县| 大姚县| 库车县| 麦盖提县| 易门县| 仙居县| 宝清县| 蒲江县| 剑河县| 增城市| 松潘县| 龙南县| 阜新市| 大埔区| 新郑市| 临夏县| 东至县| 什邡市| 仪征市| 德阳市| 长阳| 宜春市| 阳新县| 布尔津县| 乌鲁木齐市| 锡林浩特市| 汉阴县| 汝城县| 阿拉善右旗| 九龙县| 永济市| 盘山县| 偃师市| 晋城| 文登市| 普安县| 本溪| 林甸县| 固原市| 邮箱| 泉州市|