当前位置: 首页 > Web前端 > HTML5

讲解TypeScript的基本用法

时间:2023-04-05 15:00:08 HTML5

TypeScript是微软开发的JavaScript的超集。TypeScript与JavaScript兼容,可以加载JavaScript代码然后运行。与JavaScript相比,TypeScript的进步包括:加入注解,让编译器理解支持的对象和函数,编译器会移除注解,不会增加开销;添加一个完整的类结构来更新它是传统面向对象语言JavaScript与TypeScript的区别TypeScript是JavaScript的超集,它扩展了JavaScript的语法,因此现有的JavaScript代码无需任何修改就可以与TypeScript一起工作。TypeScript通过类型注释提供编译时静态类型检查。TypeScript可以处理现有的JavaScript代码,并且只编译其中的TypeScript代码。我决定使用EgretEngine来开发游戏,在开发游戏之前学习TypeScript。目前其可视化工具已经可以用于多端口封装。Egret是一套完整的HTML5游戏开发解决方案。Egret包含多个工具和项目。EgretEngine是基于TypeScript语言开发的HTML5游戏引擎。项目在BSD许可下发布1.String新特性``1.Stringnewlinevarcontent=`111122222`2.Stringtemplatevarname="Mr.Yang"vargetName=function(){return"Hangzhou"}console.log(`Iam${name}from${getName()}`)//输出:IamMr.YangfromHangzhouStringtemplateYoucanusevariablesoryoucanusefunctioncallstoautomaticallysplitstringsfunctiontest(tp,name,age){console.log(tp)console.log(name)console.log(age)}varmyname='先生。sheep'vargetAge=function(){return18;}test`我是${myname},今年${getAge()}`返回:2.参数新特性1.参数类型:参数后面加冒号要指定的参数名称参数类型varname:string='Mr.sheep'//第二次赋值只能给字符串赋值varalias:any='all'//any可以给字符赋任何类型varage:bumber='25'//再次赋值只能赋值为anumbervarman:boolean=true//重新赋值只能赋为布尔值functiontest():void{}//void声明函数不返回值functiontest():string{}//声明function只能返回字符串类型functiontest(name:string):string{}/为函数的参数声明类型。除了为变量声明类型外,还可以为函数声明类型,也为函数的参数声明类型2.自定义类型类Person{name:string;age:number;}varname:Person=newPerson();//如果用编辑器写,会提示类型,方便开发,易于管理3.参数默认值functiontest(a:string,b:string,s:string='C'){console.log(a)console.log(b)console.log(c)}test('A','B','C')//输出ABCtest('A','B')//输出ABC注意:后面声明了带默认值的参数,因为在typescript中,参数没有全传,会抛出异常4.可选参数functiontest(a:string,b?:string,s:string='C'){console.log(a)console.log(b)console.log(c)}test('A')//OutputAundefinedC注意:同理,可选参数声明在必选参数之后5.Operator-wirelessParametersfunctiontest(...args){args.forEach(function(arg){consloe.log(arg)})}test(1,2,3)//输出123test(1,2,3,4,5)//输出123456.Operator-固定参数functiontest(a,b,c){console.log(a)console.log(b)console.log(c)}varargs=[1,2]test(...args)//输出12undefinedvarargs2=[4,5,6,7,8,9]test(...args2)//输出4567。生成器函数function*test(){console.log(1)yield;console.logg(2)}varfun1=test();fun1.next();//使用next,输出1fun1.next();//调用一次Output2function*test(){while(true){yieldMath.random()*1000}}varp=test()varlimitPrice=15varprice=100while(price>limitPrice){price=p.下一个()。价值控制台。日志(`随机数${price}`)}console.log(`跳出时的随机数${price}`)随机输出:8.析构表达式-对象函数test(){return{code:'100',peice:'1000',obj:{a:2000,b:3000}}}var{code,peice}=test()console.log(code)//输出100console.log(peice)//输出1000var{code:codex,peice}=test()console.log(codex)//输出100console.log(peice)//输出1000var{code,peice,obj}=test()console.log(obj)//输出对象var{code,peice,obj:b}=test()console.log(b)//输出3000注意:解析出来的变量variable与函数返回的值相同9.析构表达式-数组vararr1=[1,2,3,4]var[number1,number2]=arr1console.log(number1)//输出1console.log(number2)//输出2var[,,number1,number2]=arr1console.log(number1)//输出3console.log(number2)//输出4var[number1,,,number2]=arr1console.log(number1)//输出1console.log(number2)//输出4var[number1,number2,...others]=arr1console.log(number1)//输出1console.log(number2)//输出2console.log(others)//输出34使用函数test([number1,number2,...others]){console.log(number1)console.log(number2)console.log(others)}test(arr1)//输出12[3,4]析构表达式让你少写代码3.箭头表达式1.单行箭头:varfoo=(arg1,arg2)=>arg1+arg2;//上面的表达式等价于varfoo=function(arg1,arg2){returnarg1+arg2;};2.无参数:varfoo=()=>{}//上面的表达式等价于varfoo=function(){}单参数varfn=x=>x*x;this指向varobj={birth:1990,getAge:function(){varb=this.birth;//1990varfn=()=>newDate().getFullYear()-this.birth;//this指向obj对象returnfn();}};obj.getAge();//25使用箭头函数使代码更易读易懂3.forin和forofforinvarmyArray=[1,2,4,5,6,7]myArray.name="Mr.Sheep"for(varindexinmyArray){控制台。log(myArray[index]);}forofvarmyArray=[1,2,4,5,6,7]myArray.name="Mr.Sheep";for(varvalueofmyArray){console.log(value);}forin遍历数组的索引(即键名),而forof遍历数组元素值forof只遍历数组中的元素,不包括数组的索引name4。Class1.创建一个类classTest{name;eat(){console.log('这是一个类')}}varp1=newTest();p1.name='a'p1.eat()varp2=newTest();p2.name='b'p2.eat()在同一个类中,可以有多个新的真实列,多个真实列都有相同的属性和方法,只是状态不同。访问控制字符classTest{privatename;//私有属性的声明publiceat(){//公共方法的声明console.log('Thisisaclass')}}class创建时,通过publicprivate关键字声明私有default,和protected只能在类内部使用。Protected只在类的类和子类中使用。classTest{nameconstructor(){this.name='Initializenamevalue'}或者这样写=>//constructor(publicname:string='initializenamevalue'){////}eat(){console.log(this.name)}}constructor(constructor)只会调用一次类的继承//parentclassTest{constructor(publicname:string){this.name='initializenamevalue'}eat(){console.log('我是父类函数')}}//子类classPestextendsTest{constructor(name:string,code:string){super(name);}tat(){super.eat()//super调用父类的函数}}varp=newPest('')p.tat()//输出:我是父类函数super1.子类的构造函数必须调用父类的构造函数。2.从超类调用父类方法Generic<>varqsts:Array=[]qsts[0]=newTest('')qsts[1]=newPest('')//这个数组onlyDatainterfacethatcanholdTesttypeinterfaceinterfaceITest(){name:string,age:number}classTest(){constructor(publicconfig:ITest){}}varp1=newTest({name:"绵羊先生",//这里定义的参数只能根据接口age:'18'})接口不仅声明了属性还声明了方法interfaceITest(){eat()}classTestextendsITest(){eat()}classPestextendsITest(){eat()}*所有实现ITest接口定制的类都必须实现eat()模块导出:用于定义模块,可以导出对象、函数、类、字符串等。:mport:used导入的也可以通过as重命名:注:平时开发中可能会看到exportdefault,所以export和exportdefault的区别在于前者导入时需要加{},后者不需要需要: