Web前端之路让前端飞Learning Typescript

Learning TypeScript 读书笔记3

2018-06-09  本文已影响8人  GunnerAha

Chap 4 TypeScript中的面向对象编程

1.面向对象开发的一些原则SOLID:

2.T雨棚script支持类class、接口interface。

3.类之间的关系:

4.支持继承特性,使用关键字extends。为了避免多继承带来的菱形问题,提供“混合”特性,用于替代多重继承。混合:

function applyMixins(derivedCtor:any, baseCtors:any[]){
   baseCtors.forEach(baseCtor=>{
       Object.getOwnPropertyNames(baseCtor.prototype).forEach(name=>{
           if(name!=='constructor'){
               derivedCtor.prototype[name]=baseCtor.prototype[name];
           }
       }
   }
}

混合的限制:

5.支持泛型类,需要声明泛型T拥有构造函数,如下写法:

function fatcory<T>():T{
    var type:{new():T;};
    return new type();//直接return new T();报找不到标识符T,编译错误
}

6.命名空间用于组织代码,属于内部模块,默认私有,需要使用export导出公共部分。例如:

namespace app{
    export class UserModel{//...}
}

命名空间支持嵌套。

7.模块:与命名空间的区别是,在声明了所有的模块之后,不会使用<script>引入,而是通过模块加载器来加载。Javascript在es6之前不支持模块,常用的模块加载器有:

对于Typescript,在编译时使用--module来标明使用哪种模块加载器器。
内部模块例子:

export class UserModel{}

8.Typescript支持ES6的模块语法:

class UserModel{}
export {UserModl}

这里export也可以写在class前面。

9.引入模块使用import:import {UserModel,xxx} from ".models";

上一篇下一篇

猜你喜欢

热点阅读