箭头函数与普通函数区别

1.this

(1)箭头函数没有this,由其定义时的上下文this决定。

(2)因此,箭头函数(是匿名函数)不能作为构造函数。

(3)箭头函数通过call()或 apply()方法调用一个函数时,只传入了一个参数,对this并没有影响。

let obj2 = {

    a: 10,

    b: function(n) {

        let f = (n) => n + this.a;

        return f(n);

    },

    c: function(n) {

        let f = (n) => n + this.a;

        let m = {

            a: 20

        };

        return f.call(m,n);

    }

};

console.log(obj2.b(1));  // 11

console.log(obj2.c(1)); // 11

(4)箭头函数没有原型属性

var a = ()=>{

  return 1;

}

function b(){

  return 2;

}

console.log(a.prototype);  // undefined

console.log(b.prototype);  // {constructor: ƒ}

2.arguments

箭头函数没有arguments,可用 ...代替

let fn = (...arg)=>{...}

你可能感兴趣的:(箭头函数与普通函数区别)