TypeScript学习笔记(七) - 命名空间
本篇将介绍TypeScript的命名空间,并简单说明一下与模块的区别。
在之前的例子里,有如下一段代码,通过修改这段代码来演示命名空间的用法。
interface Animal {
name: string;
eat(): void;
}
class Dog implements Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
eat() {
console.log(`${this.name} 吃狗粮。`);
}
}
class Cat implements Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
eat() {
console.log(`${this.name} 吃猫粮。`);
}
}
一、命名空间的声明
同Java的包、.Net的命名空间一样,TypeScript的命名空间可以将代码包裹起来,只对外暴露需要在外部访问的对象。命名空间内的对象通过export关键字对外暴露。
将上面的例子进行修改
namespace Biology {
export interface Animal {
name: string;
eat(): void;
}
export class Dog implements Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
eat() {
console.log(`${this.name} 吃狗粮。`);
}
}
export class Cat implements Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
eat() {
console.log(`${this.name} 吃猫粮。`);
}
}
}
let dog: Biology.Animal;
dog = new Biology.Dog('狗狗');
dog.eat();
通过namespace关键字声明命名空间,通过export导出需要在外部使用的对象。在命名空间外部需要通过“完全限定名”访问这些对象。
二、命名空间的引用
通常情况下,声明的命名空间代码和调用的代码不在同一个文件里
biology.ts
namespace Biology {
export interface Animal {
name: string;
eat(): void;
}
export class Dog implements Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
eat() {
console.log(`${this.name} 吃狗粮。`);
}
}
export class Cat implements Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
eat() {
console.log(`${this.name} 吃猫粮。`);
}
}
}
biology.ts
app.ts
/// <reference path="biology.ts" /> let dog: Biology.Animal;
dog = new Biology.Dog('狗狗');
dog.eat();
通过reference注释引用命名空间,即可通过“完全限定名”进行访问。
更有甚者,相同的命名空间会声明在不同的文件中
namespace Biology {
export interface Animal {
name: string;
eat(): void;
}
}
biology.ts
/// <reference path="biology.ts" />
namespace Biology {
export class Dog implements Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
eat() {
console.log(`${this.name} 吃狗粮。`);
}
}
}
dog.ts
/// <reference path="biology.ts" />
namespace Biology {
export class Cat implements Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
eat() {
console.log(`${this.name} 吃猫粮。`);
}
}
}
cat.ts
// app.ts /// <reference path="biology.ts" />
/// <reference path="cat.ts" />
/// <reference path="dog.ts" /> let dog: Biology.Animal;
dog = new Biology.Dog('狗狗');
dog.eat(); let cat: Biology.Animal;
cat = new Biology.Cat('喵星人');
cat.eat();
编译之后,每一个文件都将编译成对应的一个JavaScript文件,使用时需要将这些文件都引用进来。通过如下命令,可以将有相同命名空间的文件编译到一个JavaScript文件中,这样在引用时只需要一个文件即可。
tsc --outFile js\biology.js ts\biology.ts ts\dog.ts ts\cat.ts
将biology.ts、dog.ts、cat.ts编辑到一个JavaScript文件biology.js文件内,编译后的文件内容如下
/// <reference path="biology.ts" />
var Biology;
(function (Biology) {
var Dog = (function () {
function Dog(theName) {
this.name = theName;
}
Dog.prototype.eat = function () {
console.log(this.name + " \u5403\u72D7\u7CAE\u3002");
};
return Dog;
}());
Biology.Dog = Dog;
})(Biology || (Biology = {}));
/// <reference path="biology.ts" />
var Biology;
(function (Biology) {
var Cat = (function () {
function Cat(theName) {
this.name = theName;
}
Cat.prototype.eat = function () {
console.log(this.name + " \u5403\u732B\u7CAE\u3002");
};
return Cat;
}());
Biology.Cat = Cat;
})(Biology || (Biology = {}));
三、命名空间的别名
在引用命名空间时,可以通过import关键字起一个别名
// app.ts /// <reference path="biology.ts" />
/// <reference path="cat.ts" />
/// <reference path="dog.ts" /> import bio_other = Biology; // 别名 let dog: bio_other.Animal;
dog = new bio_other.Dog('狗狗');
dog.eat(); let cat: bio_other.Animal;
cat = new bio_other.Cat('喵星人');
cat.eat();
四、命名空间与模块
命名空间:代码层面的归类和管理。将有相似功能的代码都归一到同一个空间下进行管理,方便其他代码引用。更多的是侧重代码的复用。
模块:一个完整功能的封装,对外提供的是一个具有完整功能的功能包,需要显式引用。一个模块里可能会有多个命名空间。
TypeScript学习笔记(七) - 命名空间的更多相关文章
- Typescript 学习笔记七:泛型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记六:接口
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记五:类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记四:回忆ES5 中的类
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记二:数据类型
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记三:函数
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- Typescript 学习笔记一:介绍、安装、编译
前言 整理了一下 Typescript 的学习笔记,方便后期遗忘某个知识点的时候,快速回忆. 为了避免凌乱,用 gitbook 结合 marketdown 整理的. github地址是:ts-gitb ...
- TypeScript学习笔记(八):1.5版本之后的模块和命名空间
我之前有写过TS1.5版本之前的“模块”的笔记:TypeScript学习笔记(七):模块 但是TS这里的模块和在ECMAScript 2015里的模块(即JS原生支持了模块的概念)概率出现了混淆,所以 ...
- 【opencv学习笔记七】访问图像中的像素与图像亮度对比度调整
今天我们来看一下如何访问图像的像素,以及如何改变图像的亮度与对比度. 在之前我们先来看一下图像矩阵数据的排列方式.我们以一个简单的矩阵来说明: 对单通道图像排列如下: 对于双通道图像排列如下: 那么对 ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
随机推荐
- php array_map array_filter sort
array_map — Applies the callback to the elements of the given arrays (处理映射) array_filter — Filters e ...
- 算法第四版 在Linux 中调用Algs4库
一: 搭建Java 环境 : 确认版本: 1.8及以上 [username:~/] javac -version javac 1.8.0_111 [username:~/] java -versi ...
- 2018-2019 ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred) Solution
A. Find a Number Solved By 2017212212083 题意:$找一个最小的n使得n % d == 0 并且 n 的每一位数字加起来之和为s$ 思路: 定义一个二元组$< ...
- An error occurred: No action handlers found - check JMeterHome and libraries
An error occurred: No action handlers found - check JMeterHome and libraries Writing log file to: D: ...
- 能否通过六面照片构建3D模型?比如人脸,全身的多角度照片,生成3D模型。?
https://www.zhihu.com/question/36412840 9023 添加评论 分享 邀请回答举报 收起 已关注写回答 9 个回答 默认排序 叛逆者 计算机图形学 ...
- Python面试题目之字典排序
按照字典的内的年龄排序 待排序的字典 d1 = [ {'name':'alice', 'age':38}, {'name':'bob', 'age':18}, {'name':'Carl', 'age ...
- 清理tomcat日志大的文件
先看一个命令: [root@weblogic logs]# catalina.--.log icatalina.--:-.out icatalina.--:-.out localhost_access ...
- 20162314 《Program Design & Data Structures》Learning Summary Of The Eighth Week
20162314 2017-2018-1 <Program Design & Data Structures>Learning Summary Of The Eighth Week ...
- g711u与g729比较编码格式
•g711a—编解码格式为G.711 alaw •g711u—编解码格式为G.711 ulaw (the default) •g729—编解码格式为G.729 •g729a—编解码格式为G.729a ...
- 获取本机IP,返回字符串
public static String GetLocalIp() { String[] Ips = GetLocalIpAddress(); foreach (String ip in Ips) i ...