0511TS
var arr:Array<number>=[1,2,3,4];//创建一个数组
元组
var list:[string,number,boolean,boolean,number,string]=["a",1,true,false,2,"b"];
枚举
enum COLOR{RED,GREEN,BLUE};
对象a1b2函数
interface IObj{
a:number;
b:number;
c():void;
}
接口 必填可选只读
interface IObj{
readonly a:number;
b:string;
c?:number;
}
对象任意加属性
interface IObj{
[key:string]:number|string|undefined|boolean;
}
函数a,b,返回a+b
interface IFn{
(a:number,b:number):number;
}
var fn:IFn=<IFn>function(a,b){
return a+b;
}
定义一个update接口,用Boximplements这个接口
interface Iupdate{
update():void;
}
class Box implements IUpdate{
update():void{
console.log("a");
}
}
0514TS
1.交叉类型 &举例
interface IA{
a:number;
}
interface IB{
b:number;
}
var a:IA & IB={a:1,b:2};
2.接口和别名的区别
1.别名当鼠标划过时可以显示具体代码,接口不会;
2.接口可以继承别名,别名不能继承接口;
3.类方法装饰器
function supers(){
return function(thisArg:Box,methodName:keyof Box,valueDesc:PropertyDescriptor){ //参数的东西都是看输出观察的
var fn=valueDesc.value;
// thisArg是原型链原型对象
// valueDesc 就是当前装饰方法,现在重新play方法
valueDesc.value=function(){ //这里重写play方法
console.log("begin Play")
// console.log(this);//这个this是实例化对象的this
fn.call(this);
}
}
}
class Box{
public age:number=30;
constructor(){
}
@supers() //重写这个类的方法 写在这个地方
public play():void{
console.log("play"+this.age)
}
}
4.TimeManager
export interface IUpdate{
update():void; //声明函数,无返回值
}
export default class TimeManager{
private static _instance:TimeManager;
private list:Set<IUpdate>=new Set();
private ids?:number; //设置时间
private constructor(){
}
// 静态方法用.调用
// 运用单例实现只生成一个 _instance 也是TimeManager类
public static get instance():TimeManager
{
return TimeManager._instance || (TimeManager._instance=new TimeManager());
}
public add(item:IUpdate):void
{
this.list.add(item);
if(this.list.size>0 && !this.ids) this.ids=setInterval(()=>this.dispatch(),16) //箭头函数调用本class中方法
}
public remove(item:IUpdate):void
{
if(!this.list.has(item)) return;
this.list.delete(item);
if(this.list.size===0 && this.ids){
clearInterval(this.ids);
this.ids=undefined;
}
}
public dispatch():void{
// this.list.forEach((item:IUpdate)=>{
// item.update();
// })
this.list.forEach((item:IUpdate)=>item.update()); //执行更新之后的update
}
}