在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
认识箭头函数 var fun1 = function(a:number, b:number=2):number{ return a+b; } function fun2(a:number, b:number):number { return a+b; } //使用ES6箭头函数语法定义函数,将原函数的“function”关键字和函数名都删掉, //并使用“=>”连接参数列表和函数体。 var fun3 = (a:number, b:number):number=>{ return a+b; } (a:number,b:number):number =>{ return a+b; } //===================无参 var fun5 = function(){ //do something. } var fun6 = ()=>{}; //=================单个参数 var fun7 = function(a:number){} var fun8= (a:number) =>{} //var fun9 = a => {}; ///js ok,但是ts下面会编译报错 //如果只有一个表达式,可以省略 "{}" 和 "return" ///如果包含多条语句,不能省略 "{}" 和 "return" let fun9 = ()=> "hello"; let fun10 = ()=> {return 'hello'}; let fun11 = (a:number, b:number)=> a+b; let fun12 = (a:number, b:number)=> {return a+b}; let fun13 = (a:number)=>{ a=a+1; return a; }; // fun14 => {key:x}; //y => ({key:y}); (x:number) => ({key:x}); let fun15 = (x:number) => ({key:x}); console.dir(fun15); let fun16 = (a:number)=>{ a ++; return a; } fun16(100); console.log(fun16(100)); //=================================== var Person = { firstName:"hello", lastName:"world", getFullName:function(firstName:string){ console.log(this); var first = this.firstName var fn = (f:string)=>{ console.log(this); return f+this.lastName; } return fn.call({firstName:'hh'}, firstName); } } console.log(Person.getFullName('hi')); /// hiworld //=================================== var obj = { array:[1,2,3], sum:()=>{ console.log("sum:"); return (i:number,j:number)=>i+j; } } let fun17 = obj.sum(); //console.log(fun17(5,6)); console.log(obj.sum()(2,3)); // 5
|
请发表评论