Chrome手动同步
- 打开网址:
chrome://sync-internals/
- 点击 Disable Sync (Clear Data)
- 点击 Request Start
chrome://sync-internals/
默认情况下就是数字枚举
enum Gender {
Male,
Female
}
console.log(Gender.Male);
console.log(Gender.Female);
数字枚举注意点:
数字枚举的取值默认从 0 开始递增
enum Gender {
Male,
Female
}
console.log(Gender.Male);
console.log(Gender.Female);
数字枚举的取值可以是字面量, 也可以是常量, 也可以是计算的结果
字面量:
enum Gender {
Male = 1,
Female
}
console.log(Gender.Male);
console.log(Gender.Female);
常量,如果使用常量给前面的枚举值赋值了, 那么后面的枚举值也需要手动的赋值:
const num = 666;
enum Gender {
Male = num,
Female = 1
}
console.log(Gender.Male);
console.log(Gender.Female);
计算的结果,如果使用计算结果给前面的枚举值赋值了, 那么后面的枚举值也需要手动的赋值:
function getNum() {
return 888;
}
enum Gender {
Male = getNum(),
Female = 1
}
console.log(Gender.Male);
console.log(Gender.Female);
可以根据枚举值获取到原始值
enum Gender {
Male,
Female
}
console.log(Gender.Male);
也可以根据原始值获取到枚举值
enum Gender {
Male,
Female
}
console.log(Gender[0]);
如果使用字符串给前面的枚举值赋值了, 那么后面的枚举值也必须手动赋值
enum Gender {
Male = 'www.it6666.top',
Female = 'BNTang'
}
console.log(Gender.Male);
console.log(Gender.Female);
字符串枚举注意点
enum Gender {
Male = 'BNTang',
Yao = Male
}
console.log(Gender.Male);
console.log(Gender.Yao);
枚举中既包含数字又包含字符串, 我们就称之为 异构枚举
enum Gender {
Male = 6,
Female = 'nv'
}
console.log(Gender.Male);
console.log(Gender.Female);
注意点:如果是字符串枚举, 那么无法通过原始值获取到枚举值:
enum Gender {
Male = 6,
Female = 'nv'
}
console.log(Gender['nv']);
console.log(Gender);
var demo: boolean = true; // 定义一个布尔类型的变量
var arr1: number[] = [1, 2, 3];
var arr2: Array<number> = [1, 2, 3];
var t: [string, number, boolean] = ["a", 1, true]
enum Flag {
success = 1,
error = -1
}
var f: Flag = Flag.success;
如果声明枚举的时候不赋值, 则输出的值是索引值, 索引从0开始
enum Flag {a, b, c}
console.log(Flag.b); // 这里输出1
var a: number | boolean ;
a = 123;
a = true;
如果您看到这篇文章,表示您的 blog 已经安装成功.