TypeScript
1.枚举emun
enum Sex { //默认元素的下标从0开始递增(可指定) man = 1, woman = 2 } let sex1: Sex = Sex.man; let sex2: Sex = Sex.woman;
console.log(sex1, sex2);
let man:string = Sex[1];
let woman:string = Sex[2];
console.log(man, woman);
//编译结果 var Sex; (function (Sex) { Sex[Sex["man"] = 1] = "man"; Sex[Sex["woman"] = 2] = "woman"; })(Sex || (Sex = {}));
var sex1 = Sex.man;
var sex2 = Sex.woman;
console.log(sex1, sex2); //1 2
var man = Sex[1];
var woman = Sex[2];
console.log(man, woman); //man woman