关于arguments的问题

来源:16-9 手写节流 throttle

光天化日之下

2020-01-08

setTimeout里面为什么用箭头函数 arguments才生效,用function () {} 就会报错

index.js:35 Uncaught TypeError: Cannot read property ‘offsetX’ of undefined

function throttle (func, wait = 100) {
  var timer = null
  return function () {
    if (timer) {
      return
    }
    timer = setTimeout(function () { 
	// 这里如果不用箭头函数,arguments不起作用
      func.apply(this, arguments)
      timer = null
    }, wait)
  }
}
写回答

1回答

双越

2020-01-08

针对箭头函数和 function 两种情况,你分别打印一下 arguments 的值,看输出什么。

0
1
光天化日之下
function a () { console.log(arguments) } const b = () => { console.log(arguments) } a(1) // Arguments [1, callee: ƒ, Symbol(Symbol.iterator): ƒ] b(1) // Uncaught ReferenceError: arguments is not defined 看了阮一峰的ECMAScript 6 入门关于箭头函数的注意事项: (3)不可以使用arguments对象,该对象在函数体内不存在。 如果要用,可以用 rest 参数代替。 明白了,谢谢老师!
2020-01-08
共1条回复

一天时间高效准备前端技术一面 匹配大厂面试要求

针对时下面试高频考点,帮助新人js面试快速通关

4694 学习 · 1681 问题

查看课程