• 技术文章 >web前端 >前端问答

    实例代码之ES6箭头函数实践

    长期闲置长期闲置2022-08-08 10:26:02转载88
    本篇文章给大家带来了关于javascript的相关知识,其中主要介绍了箭头函数应用的相关问题,箭头函数表达式更适用于那些本来需要匿名函数的地方,并且它不能用作构造函数,下面一起来看一下,希望对大家有帮助。

    【相关推荐:javascript视频教程web前端

    简介

    箭头函数表达式的语法比函数表达式更简洁,并且没有自己的this,arguments,super或new.target。箭头函数表达式更适用于那些本来需要匿名函数的地方,并且它不能用作构造函数。 ---- MDN

    基础用法

    参数表示

    (param1, param2, …, paramN) => { statements }
    (param1, param2, …, paramN) => expression
    //相当于:(param1, param2, …, paramN) =>{ return expression; }
    
    // 当只有一个参数时,圆括号是可选的:
    (singleParam) => { statements }
    singleParam => { statements }
    
    // 没有参数的函数应该写成一对圆括号。
    () => { statements }

    返回值表示

    let add1 = (num1, num2) => {
      num1 + num2
    };
    let add2 = (num1, num2) => {
      return num1 + num2
    };
    let add3 = (num1, num2) => (num1 + num2);
    let add4 = (num1, num2) => num1 + num2;
    
    console.log(add1(2, 3));  // undefined
    console.log(add2(2, 3)); // 5
    console.log(add3(2, 3)); // 5
    console.log(add4(2, 3)); // 5

    进阶

    //加括号的函数体返回对象字面量表达式:
    let func = () => ({foo: 'bar'})
    console.log(func()); // {foo: 'bar'}
    
    
    //支持剩余参数和默认参数
    (param1, param2, ...rest) => { statements }
    (param1 = defaultValue1, param2, …, paramN = defaultValueN) => {
    statements }
    
    //同样支持参数列表解构
    let f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c;
    f();  // 6

    this

    在这里插入图片描述

    最佳实践

    why?语法更简洁,并且this更符合预期
    如果函数逻辑相当复杂,应当使用命名函数

    // bad[1, 2, 3].map(function (x) {
      const y = x + 1;
      return x * y;});// good[1, 2, 3].map((x) => {
      const y = x + 1;
      return x * y;});
    // bad
    [1, 2, 3].map(number => {
      const nextNumber = number + 1;
      `A string containing the ${nextNumber}.`;
    });
    
    // good
    [1, 2, 3].map(number => `A string containing the ${number}.`);
    
    // good
    [1, 2, 3].map((number) => {
      const nextNumber = number + 1;
      return `A string containing the ${nextNumber}.`;
    });
    
    // good
    [1, 2, 3].map((number, index) => ({
      [index]: number,
    }));
    
    // No implicit return with side effects
    function foo(callback) {
      const val = callback();
      if (val === true) {
        // Do something if callback returns true
      }
    }
    
    let bool = false;
    
    // bad
    foo(() => bool = true);
    
    // good
    foo(() => {
      bool = true;
    });

    why?函数开头和结束更明确

    // bad['get', 'post', 'put'].map(httpMethod => Object.prototype.hasOwnProperty.call(
        httpMagicObjectWithAVeryLongName,
        httpMethod,
      ));// good['get', 'post', 'put'].map(httpMethod => (
      Object.prototype.hasOwnProperty.call(
        httpMagicObjectWithAVeryLongName,
        httpMethod,
      )));
    // bad[1, 2, 3].map((x) => x * x);// good[1, 2, 3].map(x => x * x);// good[1, 2, 3].map(number => (
      `A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`));// bad[1, 2, 3].map(x => {
      const y = x + 1;
      return x * y;});// good[1, 2, 3].map((x) => {
      const y = x + 1;
      return x * y;});
    // badconst itemHeight = item => item.height > 256 ? item.largeSize : item.smallSize;// badconst itemHeight = (item) => item.height > 256 ? item.largeSize : item.smallSize;// goodconst itemHeight = item => (item.height > 256 ? item.largeSize : item.smallSize);// goodconst itemHeight = (item) => {
      const { height, largeSize, smallSize } = item;
      return height > 256 ? largeSize : smallSize;

    简单结论

    【相关推荐:javascript视频教程web前端

    以上就是实例代码之ES6箭头函数实践的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:CSDN,如有侵犯,请联系admin@php.cn删除
    专题推荐:ES6
    上一篇:ES6中箭头函数的详细梳理 下一篇:ES6箭头函数及this指向详解
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• es6数组怎么删除第一个元素• ES6数组去重的5种方法是什么• es6怎么删除数组中的元素• es6怎么删除数组中的某一个对象• es6怎么求数组中的奇数和
    1/1

    PHP中文网