P26-箭头函数和普通函数的区别

50 阅读1分钟
  1. this指向的区别 箭头函数中的this是在定义时就决定的,而且不可修改(call,apply,bind) 箭头函数的this指向定义时候,外层第一个普通函数的this
  2. 箭头函数不能new,不能当做构造函数。
  3. 箭头函数没有prototype
  4. 箭头函数没有arguments
  5. sdfsd
let obj = {
  a: function(){
    console.log(this); // this === obj
  },
  b: () => {
    console.log(this); // this === window
  }
}
obj.a();
obj.b();
// 闭包 run普通函数 fun箭头函数
let obj6 = {
  run: function(){
    return function(){
      console.log(this); // this === window
    }
  },
  fun: function(){
    return () => {
      // 箭头函数的this指向定义时候,外层第一个普通函数的this
      console.log(this); // this === obj6
    }
  }
}
obj6.run()();
obj6.fun()();
obj6.run().call(obj6); // 可以修改this指向
obj6.fun().call(window); // 不能修改this指向 
// 箭头函数不能new
let bun = () => {
  return 'ssss';
}
// console.log(new bun()); // 报错

// 箭头函数没有prototype
console.log(bun.prototype); // undefined
// 普通函数有arguments
let fun = function() {
  console.log(arguments);
}
fun(1);

// 箭头函数没有arguments
let run = () => {
  console.log(arguments); // 报错
}
run();