TypeScript深刻学习

  • 基础类型
    boolean
    number
    string
    string[]//Array<string> 数组类型(ReadonlyArray<string>数组不能修改,也不容许被赋值给其余)
    tuple 元组,也就是一个集合[string,number]
    enum枚举
    显示下标

    any任意类型
    void函数没有返回值数组

  • 接口
    一、接口初探
    对象参数严格或宽松检查
    二、可选属性
    添加?

    三、只读属性readonly
    readonly数组能够被修改
    函数

    四、额外的属性检查
    as X 鸭子模型(不可靠,由于内部引不到,没啥用)
    this

    对象从新赋值
    spa

    字符串索引签名
    code

    [propName:string]:any
    四、函数类型
    对象

    (x:string):boolean函数的参数类型和返回值类型
    六、可索引的类型
    blog

    七、类类型
    实现接口
    implements(以前有提到过,这里就不作解释了)继承


  • 类、继承
    在继承的时候若是子类没有constructor,那就读取父类的constructor,可是若是父类的constructor须要传参那就必须传一下,子类有constructor就必须写super(),若是父类须要参数还要传过去,不要就空着就能够了。
    class Person {
        //成员变量
        name: string
        //构造函数
        constructor(name: string) {
            this.name=name
        }
        //成员函数
        eat():void {
            console.log(this.name)
        }
    }
    class Student extends Person{
        id: number;
        constructor(name:string,id:number) {
            super(name)
        }
        study() {
            console.log(this.id)
        }
    }
    
    const person = new Person("hou")
    console.log(person.name)
    person.eat()
    
    const student = new Student("hh",4)
    console.log(student.name)
    console.log(student.id)
  • 修饰符
    一、public:成员能够在子类访问到,实例上也能够访问到
    二、private:只能在类本身内部访问
    三、protected:被保护的类型,在子类访问到,可是实例上访问不到

  • 抽象类
    abstract,不能被实例化,只能被继承
相关文章
相关标签/搜索