zoukankan      html  css  js  c++  java
  • es6 箭头函数(arrow function) 学习笔记

    箭头函数有两个好处。☕

    1.他们比传统函数表达式简洁。

    const arr = [1, 2, 3];
    const squares = arr.map(x => x * x);
    
    // 传统函数表达式:
    const squares = arr.map(function (x) { return x * x });

    2.箭头函数不会绑定关键字this,我们不需要用bind()或者that = this这种方法了

    function UiComponent() {
        const button = document.getElementById('myButton');
        button.addEventListener('click', () => {
            console.log('CLICK');
            this.handleClick(); // this不再是button
        });
    }

    和this同样没有被箭头函数绑定的参数有

    • arguments
    • super
    • this
    • new.target

    例如:

    function foo() {
        setTimeout( () => {
            console.log("args:", arguments);
        },100);
    }
    
    foo( 2, 4, 6, 8 );
    // args: [2, 4, 6, 8]

    =>箭头函数并没有绑定 arguments,所以它会以 foo() 的 arguments 来取而代之,而 super 和 new.target 也是一样的情况

    传统的函数不好的地方在于有一些非方法的函数,劫持了this

    在JavaScript中,传统函数有3种角色:

    1.非方法函数

    2.方法

    3.构造函数

    这三种函数在使用中会有角色冲突,角色2、3的函数含有他们自己的this指向,但是角色1会对this进行劫持

    你可以看这段传统JS代码:

    function Prefixer(pp) {
        this.prefix = pp;
    }
    Prefixer.prototype.prefixArray = function (arr) { // (A)
        return arr.map(function (x) { // (B)
            // 无法正常工作:
            return this.prefix + x; // (C)
        });
    };
    var pre = new Prefixer('Hi ');
    pre.prefixArray(['Joe', 'Alex']);//TypeError: Cannot read property 'prefix' of undefined

    在C行,我们希望访问this.prefix,但是却无法访问,因为this在非方法函数里是undefined,所以我们会得到报错。

    在ECMAScript5中,有三种办法可以绕过这个问题:

    方案1 that = this

    function Prefixer(prefix) {
        this.prefix = prefix;
    }
    Prefixer.prototype.prefixArray = function (arr) {
        var that = this; // (A)
        return arr.map(function (x) {
            return that.prefix + x;
        });
    };
    
    
    var pre = new Prefixer('Hi ');
    pre.prefixArray(['Joe', 'Alex']); //[ 'Hi Joe', 'Hi Alex' ]

    方案2 直接指定this的值

    一部分数组方法会提供一个额外的参数值来修改this,注意A行

    function Prefixer(prefix) {
        this.prefix = prefix;
    }
    Prefixer.prototype.prefixArray = function (arr) {
        return arr.map(function (x) {
            return this.prefix + x;
        }, this); // (A)
    };

    方案3 绑定this

    你可以使用bind()方法来告诉函数是谁调用的

    function Prefixer(prefix) {
        this.prefix = prefix;
    }
    Prefixer.prototype.prefixArray = function (arr) {
        return arr.map(function (x) {
            return this.prefix + x;
        }.bind(this)); // (A)
    };

    ECMAScript6 方案:箭头函数

    箭头函数的工作方式很像方案3,但箭头函数压根不会修改this的作用域,而且你不需要给正常的函数加绑定。

    下面是用了箭头函数后的代码:

    function Prefixer(prefix) {
        this.prefix = prefix;
    }
    Prefixer.prototype.prefixArray = function (arr) {
        return arr.map((x) => {
            return this.prefix + x;
        });
    };

    如果完全用ES6写这段代码,可以写成这样,可以用一个类和一个更节省的方法写函数:

    class Prefixer {
        constructor(prefix) {
            this.prefix = prefix;
        }
        prefixArray(arr) {
            return arr.map(x => this.prefix + x); // (A)
        }
    }

    在A行,我们通过箭头函数省略了一些字符

    • 如果函数只有一个参数(x),那么可以省略括号
    • 如果函数体只有一个表达式,那么可以省略return

    箭头函数语法

    参数:

        () => { ... } // 没有参数
         x => { ... } // 一个参数
    (x, y) => { ... } // 多个参数

    函数体:

    x => { return x * x }  // 语句块
    x => x * x  // 表达式,和上一行作用相同

    如果函数体是一个表达式,会隐式返回结果,对比一下:

    const squares = [1, 2, 3].map(function (x) { return x * x });
    const squares = [1, 2, 3].map(x => x * x);

    单个参数时括号可以省略

    只有一个函数参数的时候,才可以省略小括号:

    [1,2,3].map(x => 2 * x) //[ 2, 4, 6 ]

    如果有多个参数的时候,就必须加上小括号:

    [[1,2], [3,4]].map(([a,b]) => a + b) //[ 3, 7 ]

    当你使用ES6函数参数默认值的时候也必须加括号:

    [1, undefined, 3].map((x='yes') => x) //[1, yes', 3 ]

    箭头函数的优先级比较低

    如果你把箭头函数当成一个运算符的话,它会有比较低的优先级。

    所以当箭头函数和其他运算符相遇时,其他运算符通常会赢。

    例如下面示例,箭头函数和条件表达式挨在一起了:

    const f = x => (x % 2) === 0 ? x : 0;

    换句话说等同于下面的语句:

    const f = x => ((x % 2) === 0 ? x : 0);

    如果想提高箭头函数的优先级,可以使用括号括住函数,让函数返回结果作为条件:

    const f = (x => ((x % 2) === 0)) ? x : 0;

    另一方面,当你使用使用typeof这样的表达式作为函数体时,不需要添加括号

    const f = x => typeof x;

    关于箭头函数换行

    es6禁止参数定义和箭头之间的换行符

    const func1 = (x, y) // SyntaxError
    => {
        return x + y;
    };
    const func2 = (x, y) => // OK
    {
        return x + y;
    };
    const func3 = (x, y) => { // OK
        return x + y;
    };
    
    const func4 = (x, y) // SyntaxError
    => x + y;
    const func5 = (x, y) => // OK
    x + y;

    在参数中换行是可以的:

    const func6 = ( // OK
        x,
        y
    ) => {
        return x + y;
    };

    关于箭头函数的函数体

    函数体内只有一个表达式是可以省略大括号的:

    asyncFunc.then(x => console.log(x));

    但是声明必须放在大括号中:

    asyncFunc.catch(x => { throw x });

    返回对象变量

    如果想让函数返回一个花括号对象,就要加小括号:

    const f1 = x => ({ bar: 123 });
    f1();//{ bar: 123 }

    不然的话,程序会认为这是一个块级作用域

    const f2 = x => { bar: 123 };
    > f2() //undefined

    立即调用的箭头函数

    还记得立即调用函数表达式(IIFE)吗? 它们如下所示,用于模拟ECMAScript 5中的块范围和值返回块:

    (function () { // open IIFE
        // inside IIFE
    })(); // close IIFE

    你也可以用箭头函数模拟:

    (() => {
        return 123
    })()

    提取方法

    <a id="myButton">按钮</a>
    <script type="text/javascript" src="js/babel.min.js"></script>
    <script type="text/babel">
        class KK {
            constructor() {
                var myButton = document.getElementById('myButton');
                myButton.addEventListener('click', event => this.handleEvent(event));
                //es5 myButton.addEventListener('click', this.handleEvent.bind(this));
            }
    
            handleEvent(event) {
                console.log(event);
            }
        }
    
        var kk = new KK();
    </script>

    箭头函数还可以用于参数指定技巧

    比如使用array.filter需要传入函数,而且需要指定this对象为bs,只能通过额外参数来指定

    const as = new Set([1, 2, 3]);
    const bs = new Set([3, 2, 4]);
    const intersection = [...as].filter(bs.has, bs);// [2, 3]

    可以用箭头函数优化写法:

    const as = new Set([1, 2, 3]);
    const bs = new Set([3, 2, 4]);
    const intersection = [...as].filter(a => bs.has(a)); // [2, 3]

    部分求值

    以前可以利用bind来产生一个新的函数来求值,但是尴尬的是第一个参数必须要传入undefined,这变得难以理解:

    function add(x, y) {
        return x + y;
    }
    const plus1 = add.bind(undefined, 1);
    console.log(plus1(2));//3

    如果用箭头函数,就变得容易理解:

    const plus1 = y => add(1, y);

    箭头函数和普通函数对比

    1.箭头函数中不会干涉下列关键字:super, this, new.target

    2.箭头函数没有构造函数:常规函数有Construct和函数属性,而箭头函数没有,所以 new (() => {}) 会报错

    除了以上两点,箭头函数和普通函数没有什么区别,typeof和instanceof输出的结果相同:

    typeof (() => {}) //'function'
    () => {} instanceof Function //true
    
    
    typeof function () {} //'function'
    function () {} instanceof Function //true

    参考资料:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

  • 相关阅读:
    EL表达式与JSTL
    JSP
    session
    四则运算 第二次
    第二次作业
    四则运算
    用户使用手册与测试报告
    系统设计和任务分配
    需求规格说明书和原型设计
    用户需求分析和竞品分析
  • 原文地址:https://www.cnblogs.com/zhishaofei/p/6622377.html
Copyright © 2011-2022 走看看