1. 枚举
使用关键词enum 来声明枚举, 枚举中默认以 ; 号结尾,值默认以 0 开始自增,
// 情况1
enum selected {
optional // 可选 值为0
unOptional // 不可选 值为1
}
// 访问
const class1 {
isOptional : selected.optional // 0 访问是和对象访问一致
}
// 情况2
enum selected {
optional = 5 // 值为5, 确定以5开始自增
unOptional // 值为6
}
// 访问
const class1 {
isOptional : selected.optional // 5 访问是和对象访问一致
}
// 情况3
enum selected {
optional = ‘yes’ // 值为yes, 值为字符串时不可自增
unOptional = ‘no’ // 值为no
}
// 访问
const class1 {
isOptional : selected.optional // yes 访问是和对象访问一致
}
2. 类访问修饰符
属性名 | 作用 |
---|---|
public | 公有属性, 随时可以访问 |
private | 私有属性, 只有类的内部成员可以访问 |
protected | 受到保护的属性, 只有类的内部或者子类成员可以访问 |
readonly | 只读属性,可以跟在上边几个属性后边 eg:public readonly cache |
3. 类与接口
使用interface 定义接口, 使用implements 去实现接口
interface Eat {
eat(foot: string) : void
}
// 可以同时实现多个接口 , 以,号分开 eg: implements Eat,Other
class Person implements Eat {
// 类中必须包含eat函数,
eat(foot: string) : void {
console.log(foot)
}
}
4. 抽象类
关键词 abstract , 抽象类只能被继承,不能通过new 去实例化。 抽象方法不需要方法体,等子类去实现父类的抽象方法
4. 类型声明
关键词 declare ,用于兼容没有声明类型的函数
declare function camelcase (input: string) : string