• TypeScript的箭头函数(第1篇)


    认识箭头函数

    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 
  • 相关阅读:
    SQLAlchemy介绍
    Flask介绍
    逆转的生殖——形而下的EOE补完仪式…
    huiyin
    实验课上
    我的博客今天1岁213天了,我领取了…
    关于直接写屏
    OceanBorn  歌曲列表
    Gethsemane
    光辉岁月-Beyond
  • 原文地址:https://www.cnblogs.com/music-liang/p/12723964.html
Copyright © 2020-2023  润新知