特点
- typeScript 是 javascript 的超集,扩展了语法
- 类 Classes
- 接口 interfaces
- 模块 Modules
- 类型注解 Type annotaions
- 编译时类型检查 Compile time type checking
- Arrow函数(类似c#的Lambda)
类型批注
function Add(left :number,right :number) :number()
{ return left+right;}
#3 接口
interface Shap{
name:string;
width:number;
color?:string;(?参数可有可无)
}
function area(myshap:Shap){ ...}
箭头函数表达式(lambda表达式)
() => 相当于 function()
() => { something }
() => something
相当于js中的函数
好处:自动将函数中的this附上上下文
编译后js文件中后会有 var _this = this;
类
class ShapP{
name:string;
...
construcor( name:string, ..) { this.name = name; ...}
print(){...}
}
var xx = new ShaP("name",...);
继承
extends 关键字
class Shap3D extends Shape{
volume:number;
..
construcor(Shape类中的所有参数...){
super(Shape类中的所有参数...)
}
shout(){
return super.(Shape类的调用);
}
}