- Expert Angular
- Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
- 157字
- 2021-07-15 17:05:33
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" />
推薦閱讀
- 觸·心:DT時代的大數據精準營銷
- Getting Started with React
- MySQL數據庫應用與管理 第2版
- Software Testing using Visual Studio 2012
- Python高級編程
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優化計算
- Xamarin.Forms Projects
- 重學Java設計模式
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- PrimeFaces Blueprints
- Java Web從入門到精通(第2版)
- Python Web自動化測試設計與實現
- The Statistics and Calculus with Python Workshop
- 實驗編程:PsychoPy從入門到精通
- Getting Started with JUCE