2

The doc here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#No_binding_of_arguments says Arrow functions do not have their own arguments object. So it should be undefined when we use it in an arrow function. But I got different results between nodejs console and the browser(Firefox Developer 70.0b2).

I searched the Internet, but cannot find official doc for this.

let arrowFun = () => {
  console.log(arguments);
};

arrowFun("a", "b", "c");
  1. In Nodejs console, I expect the output to be undefined, but the actual output is:
[Arguments] {
      '0': {},
      '1':
       { [Function: require]
         resolve: { [Function: resolve] paths: [Function: paths] },
         main:
          Module {
            id: '.',
            exports: {},
            parent: null,
            filename: 'e:\\workspace\\firefox-webide-test\\test\\app.js',
      ... omitted for brevity ...
}
  1. In browser, I got the right output: undefined
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
James
  • 157
  • 1
  • 6

1 Answers1

1

That's because NodeJS silently wraps your modules into an IIFE to provide module-scoped variables (e.g. module and exports). That's the arguments you are seeing (it's not that arguments doesn't exist in arrow functions, it is lexically scoped just like this, so in both cases it points to the surrounding arguments²).


²proof:

function surrounding() {
  (() => console.log(arguments))();
}

surrounding("you see");
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151