- 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" />
推薦閱讀
- Oracle從新手到高手
- WSO2 Developer’s Guide
- 編寫高質(zhì)量代碼:改善C程序代碼的125個(gè)建議
- Learn React with TypeScript 3
- Solr Cookbook(Third Edition)
- SQL Server實(shí)用教程(SQL Server 2008版)
- ArcGIS for Desktop Cookbook
- HTML5開(kāi)發(fā)精要與實(shí)例詳解
- R Data Science Essentials
- RubyMotion iOS Develoment Essentials
- Distributed Computing in Java 9
- Exploring SE for Android
- UI設(shè)計(jì)基礎(chǔ)培訓(xùn)教程(全彩版)
- Access數(shù)據(jù)庫(kù)應(yīng)用教程(2010版)
- Clojure Web Development Essentials