• TypeScript箭头函数(第2篇)


    let listA:number[] = [1,2,3];
    listA.push(4);
    let listB:Array<number>=[1,2,3];
    console.dir(listA);
    console.dir(listB);
    
    let mySum = function(x:number, y:number):number{
        return x+y;
    }
    console.log(mySum(1,2));
    
    let mySum2 = (x:number, y:number):number=>x+y;
    console.log(mySum2(2,3));
    
    
    ///对右侧匿名函数进行类型定义  (x:number, y:number)=>number 
    let mySum3:(x:number, y:number)=>number = function(x:number, y:number):number{
        return x+y;
    }
    console.log(mySum3(3,4));
    
    ///如果只有一条语句,不仅省掉了"{}",连 "return" 都给省掉了.  mySum4和mySum5等价
    let mySum4:(x:number, y:number)=>number = (x:number, y:number):number => x+y;
    let mySum5:(x:number, y:number)=>number = (x:number, y:number):number => { return x+y};
    console.log(mySum4(5,6));
    console.log(mySum5(5,6));
    
    let mySum6:(x:number, y:number)=>void = (x:number, y:number):void => {  
        x = x+y; 
        console.log(x);
    };
    mySum6(100,200);
    
    
    ///箭头函数返回一个对象
    let getTempData = (id:number) =>({
        userID:id,
        name:"Henry"
    });
    
    console.log(getTempData(666));
    console.log(getTempData(666).userID);
    console.log(getTempData(666).name);
    
    
    ///不带参数,有返回值
    let mySum7 = ():number=>{
        ///do something...
        return 666;
    };
    
    //不带参数,没有返回值
    let mySum8 = ()=>{
        ///do something...
        return 666;
    };
    
    function doesNotReturn()
    {
        console.log("---doesNotReturn---");
    }
    
    //不带参数,没有返回值,而且只有一条语句
    let mySum9 = () => void doesNotReturn();
    ///执行 右侧的函数.没有返回值..
    mySum9();
    
    
    const isEven = (n:number) => n % 2 === 0 ;
    const square = (n:number) => n * n;
    console.log(isEven(1));
    console.log(isEven(2));
    
    
    // 箭头函数写法
    let test1 = [1,2,3];
    console.dir(test1.map(x => x * x));
    console.dir(test1);
    
    
    
    // 正常函数写法
    let values1 = [6,4,2,1,5,3];
    let values2 = [5,3,2,1,4,6];
    var result1 = values1.sort(function (a, b) {
        return a - b;
      });
      
    // 箭头函数写法
    var result2 = values2.sort((a, b) => a - b);
    console.log(result1);
    console.log(result2);
    
    
    
    //=============嵌套的箭头函数
    let insert = (value:number) => (
        {into: (array:Array<number>) => (
            {after: (afterValue:number) => {
                array.splice(array.indexOf(afterValue) + 1, 0, value);
                return array;
            }
        }
        )
    }
    );  
    //把某个数,插入到数组中某个元素后面
    console.log(insert(5).into([4, 5, 6, 7]).after(6)); 

    参考地址:

    https://es6.ruanyifeng.com/#docs/function#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0

     https://www.jianshu.com/p/f97fc6bc0d56

  • 相关阅读:
    次小生成树
    [bzoj5329] P4606 [SDOI2018]战略游戏
    CF487E Tourists
    P3225 [HNOI2012]矿场搭建
    CF #636 (Div. 3) 对应题号CF1343
    P3469 [POI2008]BLO-Blockade
    大假期集训模拟赛12
    大假期集训模拟赛11
    大假期集训模拟赛10
    小奇画画——BFS
  • 原文地址:https://www.cnblogs.com/music-liang/p/12724636.html
Copyright © 2020-2023  润新知