0

t1 and t2 returns a function in different way, a normal function or an arrow function:

function t1(fn) {
    return async function() {
        console.log(arguments);
    }
}

function t2(fn) {
    return async () => {
        console.log(arguments);
    }
}

t1('t1: outer arg')('t1: inner arg');
t2('t2: outer arg')('t2: inner arg');

This surprised me, because I thought the normal form of the function and the arrow function notations are inter-exchangeable almost.

$ node arguments_scope.js 
[Arguments] { '0': 't1: inner arg' }
[Arguments] { '0': 't2: outer arg' }

Why?

Miao ZhiCheng
  • 617
  • 7
  • 9
  • [Don't use `arguments`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments), use `(...args)=>console.log(args)` instead. – HMR Jan 22 '19 at 12:47
  • Arrow functions, among other things, don't have their own `arguments`, so that value is coming from the closure. See e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – jonrsharpe Jan 22 '19 at 12:47

0 Answers0