十、使用命名空间

# 十、使用命名空间
命名空间和模块最好不要混用,即不要在一个模块中使用命名空间,最好在一个全局的环境使用. 在早期版本中,命名空间就是内部模块,本质上是闭包,用于隔离作用域。
namespace Shape {
const pi = Math.PI
export function cricle(r: number) {
return pi * r ** 2
}
}
/// <reference path="a.ts" />
// 防止引用a.ts报错
namespace Shape {
export function square(x: number) {
return x * x
}
}
console.log(Shape.cricle(1))
console.log(Shape.square(1))
// 命名空间的别名
import cricle = Shape.cricle
console.log(cricle(2))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
上次更新: 2021/04/22, 00:08:06