ES6快速入门 - 箭头函数

1.

// 关闭eslint
/* eslint-disable */    

{               
    // ES3,ES5
    var events = [1,2,3,4,5];
    var odds = events.map(function(v){
        return v + 1
    })
    console.log(events,odds);
}

这里写图片描述

2.

{
    // ES6
    let evens = [1,2,3,4,5];
    let odds = evens.map(v => v + 1);
    console.log(evens,odds);
}

this的区别??

{
    // 普通函数中的this指向为谁调用指向谁
    // ES3,ES5
    var factory = function(){
        this.a = 'a';
        this.b = 'b';
        this.c = {
            a : 'a+',
            b : function(){
                return this.a;
            }
        };
    }
    console.log(new factory().c.b());           //  a+
};


{
    // 箭头函数中的this的指向是定义时的指向
    // ES6
    var factory = function(){
        this.a = 'a';
        this.b = 'b';
        this.c = {
            a : 'a+',
            b : () => {
                return this.a;
            }
        }
    }
    console.log(new factory().c.b());                   //  a
}

猜你喜欢

转载自blog.csdn.net/zjsfdx/article/details/80381991
今日推荐