-1

I want to access the Arguments Object in Arrow Function.

But,

const fn = () => {
    return arguments; // error
};

It is possible if we rewrite it like this.

const fn = (...args) => (function () {
  return arguments;
})(...args);

However, I want to write a simple code first to achieve this.

I want another solution.

I think it can be achieved by making the Arrow Function look like the Function.

const arrowFunc = () => {
    return arguments;
};

function func () {
    return arguments;
}

Object.setPrototypeOf(func, arrowFunc);

arrowFunc(); // error

But it doesn't work.

Give me a new idea.

Thank you.

yaju1919
  • 1
  • 1
  • Why do you need `arguments` in an arrow function…? – deceze Jun 02 '22 at 13:50
  • Because I rewrite the library that uses the arguments object into an arrow style. – yaju1919 Jun 02 '22 at 13:56
  • Then you need to either rewrite it in a way that doesn't use `arguments`, or you *can't* rewrite it into an arrow function. – deceze Jun 02 '22 at 13:57
  • I can't do that because of the huge amount of code in the library. I need another solution. – yaju1919 Jun 02 '22 at 14:00
  • You can't have another solution. Arrow functions explicitly don't have an `arguments`. Period. See the duplicates linked above. What's wrong with simply `(...args)` instead of `arguments`? – deceze Jun 02 '22 at 14:01
  • I think it will be possible if type inference can be regarded as an arrow function as a function object. – yaju1919 Jun 02 '22 at 14:04
  • And, I know this is an evil method. Still, I want to do that. – yaju1919 Jun 02 '22 at 14:05
  • Type inference has nothing to do with it. Arrow functions don't have `arguments`, the same way they don't have their own `this`. That's one of the defining characteristics of arrow functions. Again, why not `(...args)`? – deceze Jun 02 '22 at 14:06
  • The purpose is to keep the functionality of the function object as it is, while still looking like an arrow function. – yaju1919 Jun 02 '22 at 14:09
  • You cannot simply replace all `function () { ... }` with `() => { ... }`. They're simply not interchangeable. Just because arrow functions exist does not mean you need to rewrite every single use of `function` to one. And when you do do that, you will have to change up _some_ code due to those differences. – deceze Jun 02 '22 at 14:11
  • I agree with the opinion that arrow functions and function objects are used differently depending on the situation. However, I dare not obey this time. I want to do something magical with JavaScript. – yaju1919 Jun 02 '22 at 14:15
  • Then you will truly have to perform a magic act here. Best of luck. – deceze Jun 02 '22 at 14:20

0 Answers0