当前位置:网站首页>TypeScript-枚举
TypeScript-枚举
2022-06-11 07:54:00 【YY小怪兽】
详情可见
1.数字枚举
默认情况下就是数字枚举
enum Gender{
Male,
Female
}
console.log(Gender.Male);
console.log(Gender.Female);
2.数字枚举注意点
// 数字枚举的取值默认从0开始递增
// 数字枚举的取值可以是字面量, 也可以是常量, 也可以是计算的结果
const num = 666;
function getNum() {
return 888;
}
enum Gender{
// Male = 6,
// Male = num, // 注意点: 如果使用常量给前面的枚举值赋值了, 那么后面的枚举值也需要手动的赋值
// Female = 8
Male = getNum(), // 注意点: 如果使用计算结果给前面的枚举值赋值了, 那么后面的枚举值也需要手动的赋值
Female = 123
}
3.枚举反向映射
// 可以根据枚举值获取到原始值
// 也可以根据原始值获取到枚举值
enum Gender{
Male,
Female
}
console.log(Gender.Male); // 0
console.log(Gender[0]); // Male
4.字符串枚举
enum Gender{
Male = 'www.it666.com',
Female = 'www.itzb.com' // 注意点: 如果使用字符串给前面的枚举值赋值了, 那么后面的枚举值也必须手动赋值
}
console.log(Gender.Male);
console.log(Gender.Female);
5.字符串枚举注意点
注意点: 如果使用字符串给前面的枚举值赋值了, 那么后面的枚举值也必须手动赋值
注意点: 和数字枚举不一样, 字符串枚举不能使用常量或者计算结果给枚举值赋值
注意点: 虽然字符串枚举不能够使用常量或者计算结果给枚举值赋值, 但是它可以使用内部的其它枚举值来赋值
const str = 'lnj';
function getStr() {
return 'abc';
}
enum Gender{
Male = 'www.it666.com',
// Male = str,
// Male = getStr(),
Female = 'www.itzb.com',
Yao = Female
}
console.log(Gender.Female);
console.log(Gender.Yao);
6.异构枚举
// 枚举中既包含数字又包含字符串, 我们就称之为异构枚举
enum Gender{
Male = 6,
Female = 'nv'
}
console.log(Gender.Male);
console.log(Gender.Female);
console.log(Gender[6]);
// 注意点: 如果是字符串枚举, 那么无法通过原始值获取到枚举值
// console.log(Gender['nv']);
console.log(Gender);
7.枚举成员类型
// 我们就可以把枚举成员当做类型来使用
enum Gender{
Male = 'www.it666.com',
Female = 'www.itzb.com'
}
interface TestInterface {
age: Gender.Male
}
class Person implements TestInterface{
age: Gender.Male
// age: Gender.Female // 由于类型不匹配, 所以会报错
// age: 0 // 注意点: 由于数字枚举的本质就是数值, 所以这里写一个数值也不会报错
// age: Gender.Male
// age: Gender.Female
// age: 'www.it666.com' // 注意点: 如果是字符串枚举, 那么只能是枚举成员的值, 不能是其它的值
// age: string
}
8.联合枚举类型
// 2.1什么是联合类型?
// 联合类型就是将多种数据类型通过|连接起来
// 我们可以把枚举类型当做一个联合类型来使用
let value:(number | string); // (number | string)联合类型
value = 1;
value = 6;
value = "123";
enum Gender{
Male ,
Female
}
interface TestInterface {
age: Gender // age: (Gender.Male | Gender.Female)
}
class Person implements TestInterface{
// age: Gender.Male
age: Gender.Female
}
9.运行时枚举
运行时枚举
枚举在编译之后是一个真实存储的对象, 所以可以在运行时使用
而像接口这种只是用来做约束做静态检查的代码, 编译之后是不存在的
interface TestInterface {
name:string;
age:number;
}
enum Gender{
Male,
Female
}
10.常量枚举
普通枚举和常量枚举的区别
普通枚举会生成真实存在的对象
常量枚举不会生成真实存在的对象, 而是利用枚举成员的值直接替换使用到的地方
enum Gender1{
Male,
Female
}
console.log(Gender1.Male === 0);
const enum Gender2{
Male,
Female
}
console.log(Gender2.Male === 0);
边栏推荐
- [untitled] Weng_ C lesson 1
- Magnifying mirror rendering
- 20200810 T2 dispatch money
- 2022.6.7 special student simulation
- Logical implication of functional dependence
- C language lesson 2
- Deux diplômés, la Banque a externalisé le travail d'essai pendant plus de quatre mois. Parler de vrais sentiments...
- Servlet、ServletConfig、ServletContext
- 【AtCoder2304】Cleaning
- 2021-10-17
猜你喜欢

SOCKET【5】- struct linger 用法

Deux diplômés, la Banque a externalisé le travail d'essai pendant plus de quatre mois. Parler de vrais sentiments...

Crawl Baidu Baipin dynamic page

C language - Growth Diary -01- count primes and sum

Batch splice string

Scrape captures 51job Recruitment Information (static page)

【IoT】项目管理:如何打造更好的跨职能团队?

wordcloud的使用

C language to achieve a simple game - minesweeping

You got 8K in the 3-year function test, but you were actually pretending to work hard
随机推荐
C language lesson 2
使用 COCO 数据集训练 YOLOv4-CSP 模型
JSP development model
[atcoder1983] BBQ hard (combination number + clever model transformation)
Euler's theorem and its extension (with proof)
2022.6.6 特长生模拟
Figure seamless database integration tushare interface
Classes and objects (medium)
Collation of basic knowledge of intermediate development of Andrews (for interview)
运筹学导论
Black Qunhui dsm7.0.1 physical machine installation tutorial
Servlet
Image data enhancement (translation, rotation, brightness transformation, flipping, adding Gaussian noise, scaling, cropping)
排序——归并排序
用 Keras/TensorFlow 2.9 创建深度学习模型的方法总结
C language to achieve a simple game - minesweeping
Request request object and response response object
通过ComponentCallbacks2来接收onTrimMemory等回调,并mock对应的场景
避免list的并发修改异常的几种方式
【AtCoder2387】+/- Rectangle