zoukankan      html  css  js  c++  java
  • 名词:箭头函数

    1、无参数函数

    let fn = function(){
      return 'helloWorld';
    }
    
    //简写为:
    let fn = ()=>{//但是没有参数时,括号不可以省略
      return 'helloWorld';
    }
    //根据规则二,简写为:
    let fn = ()=>'helloWorld';

    2、一个参数的函数

    let fn = function(a){
        return a;
    }
    
    //简写为:
    let fn = (a)=>{
        return a;
    }
    //根据规则一,还可以简写为:
    let fn = a=>{
        return a;
    }
    //根据规则二,还可以简写为:
    let fn = a=>a;

    3、多个参数的函数

    let fn = function(a,b){
        return a+b;
    }
    //简写为:
    let fn = (a,b)=>{//多于一个参数,圆括号不可省略
        return a+b;
    }
    //根据规则二,还可以简写为:
    let fn = (a,b)=>a+b;

    4、函数体代码多于一行

    let fn = function(){
        console.log('hello');
        console.log('world');
        return 'helloWorld';
    }
    //简写为:
    let fn = ()=>{
        console.log('hello');
        console.log('world');
        return 'helloWorld';
    }

    5、函数返回json对象时

    let fn = function(){
        return {"a":5};
    }
    
    //简写为:
    //let fn = ()=>{"a":5};这是错误的
    //应简写为:
    let fn = ()=>({"a":5});//注意{}外的圆括号。
  • 相关阅读:
    ACM模板(Java)
    【HDU 6008】Worried School(模拟)
    【HDU 6005】Pandaland(Dijkstra)
    jQuery和js使用点滴
    springmvc入门(1)
    MyBatis延迟加载和缓存(4)
    MyBatis高级映射查询(3)
    教你如何下载微信公众号的音频文件
    MyBatis代理开发(2)
    MyBatis入门程序(1)
  • 原文地址:https://www.cnblogs.com/muzhang/p/9999578.html
Copyright © 2011-2022 走看看