面试题

JavaScript中的普通函数和箭头函数的区别和用法详解

2018-04-03  本文已影响143人  飞鱼_JS
function make () {
  return ()=>{
    console.log(this);
  }
}
const testFunc = make.call({ name:'foo' });
testFunc(); //=> { name:'foo' }
testFunc.call({ name:'bar' }); //=> { name:'foo' }
function foo() {
  setTimeout( () => {
   console.log("args:", arguments);
  },100);
}
foo( 2, 4, 6, 8 );
// args: [2, 4, 6, 8]

//箭头函数中可以利用展开运算符来接收参数
const testFunc = (...args)=>{
  console.log(args) //数组形式输出参数
}
function make () {
  var self = this;
  return function () {
    console.log(self);
  }
}
//或者
function make () {
  return function () {
    console.log(this);
  }.bind(this);
}
上一篇 下一篇

猜你喜欢

热点阅读