TS(TypeScript)语法快速入门

namespace Test{//命名空间
	interface ITsInterface{//接口
		interfaceFunction();
	}
	class TsFather{//类
		//构造函数
		constructor(){
		}
		public test(){
		}
	}
	//extends-继承
	//implements-继承接口
	class TsRuleExplain extends TsFather implements ITsInterface{
		//动态类型  可以赋值为任意类型的对象
		public a;
		public b:any;
		name:string;					//string类型  不申明3p  默认public
		private age:number;				//数字类型  int  double  float byte long 都用number标示
		protected sex:boolean;			//bool变量
		public list:number[] = [];		//列表
		//对象(字典)
		//用于字典
		public map = {};//动态类型 不指明键值类型
		public map2:{[key:string]:GridItem;} = {};
		//用于对象
		public map3 = {myName:"hahah", age:1};
		//重写构造函数
		constructor(){
			//调用父类构造函数
			super();
			console.log(this.map3.myName);		//控制台打印
			console.log(this.map3.age);			//控制台打印
		}
		//重写父类方法
		public test(){
			super.test();		//重写时用于调用父类方法
		}
		//实现接口函数
		interfaceFunction(){}
		//注释的一种方式
		/**自定义传参函数 
		 * @param id 这样可以注释函数参数意思
		 * @param name 当外部调用该函数时 传参是可以看到对应注释内容
		 */
		public myFunction(id:number, name:string):number{
			return 0
		};
		//不定长参数函数  可以传入无数参数
		public myArgFunction(name:string, ...arg:any[]):void{
		};
		//常用条件判断和循环语法
		public myTest()
		{
			//if...else
			if(this.sex && this.age == 2){
			}else if( this.age == 1 || this.list.length){
			}else{
			}
			//switch
			switch(this.age){
				case 1:
					break;
				case 2:
					break;
				default:
					break;
			}
			//for循环
			for( let i = 0; i < 3; ++i ){
			}
			//这样可以循环得到map中的键
			for( let k in this.map ){
			}
			//while循环
			while(this.sex){
				if( this.sex )
					continue;
				else
					break;
			}
		}
	}
}

 

你可能感兴趣的:(H5,TS,TypeScript)