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负责把数据展示 给用户,也 ...
随机推荐
- 028-touch命令
1.创建空文件.可以创建一个空文件,也可以批量创建空文件. 2.更改文件/目录的访问时间,如果文件存在就更改访问时间,不存在就创建.# touch -a 3.更改文件的访问时间和修改时间.如果文件存在 ...
- php面向对象多继承实现
在PHP面向对象概念编程中,一个子类只能继承一个父类,但是从php5.4后新增traits实现代码复用机制变向达到多继承.Trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立 ...
- 20145326《Java程序设计》实验二Java面向对象程序设计实验报告
20145326<Java程序设计>实验二Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O. ...
- 从0开始学习 GITHUB 系列之「向GITHUB 提交代码」【转】
本文转载自:http://stormzhang.com/github/2016/06/04/learn-github-from-zero4/ 版权声明:本文为 stormzhang 原创文章,可以随意 ...
- Spring 中好用的泛型操作API
随着泛型用的越来越多,获取泛型实际类型信息的需求也会出现,如果用原生API,需要很多步操作才能获取到泛型,比如: ParameterizedType parameterizedType = (Para ...
- CSU 1963 Feed the rabbit(斜率优化dp)
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1963 题意:有m个坑,每只兔子会在ti时刻回到坑中,现在有n个人,每个人都可以从任意时间(&l ...
- Java网络编程学习A轮_07_基于Buffer的Socket编程
示例代码: https://github.com/gordonklg/study,socket module A. LineSeparate 基于 Buffer 实现逐行读取的 EchoServer ...
- JAVA synchronized关键字锁机制(中)
synchronized 锁机制简单的用法,高效的执行效率使成为解决线程安全的首选. 下面总结其特性以及使用技巧,加深对其理解. 特性: 1. Java语言的关键字,当它用来修饰一个方法或者一个代码块 ...
- 微信分享签名Java代码实现
最近写了一个小微信签名功能,记录一下希望用到的朋友可以参考下. RestController @RequestMapping("/api/wx") public class Wei ...
- python2和python3的区别——持续更新
1.在 cookbook 上看到的,python3支持 *运算符 来接收迭代变量,如: a, *b = [, , , ] python2是不支持的! 2.在 cookbook 上看到的,python3 ...