2
(function(arguments = {})
{
    console.log(arguments)
}
)("a","b","c")

prints

$ node args.js 
a
$ node --version
v8.9.4

Is there a way to access the actual arguments in that case?

0

2 Answers 2

4

I would advise against overriding the built-in arguments variable within a function definition.

You could spread the expected arguments instead using ...vargs.

(function(...vargs) {
  console.log(arguments); // Built-in arguments
  console.log(vargs);     // Variable (spread) arguments
})("a", "b", "c");
.as-console-wrapper { top: 0; max-height: 100% !important; }

Please take a look at the arguments object over at MDN for more info.

The documentation notes that if you are using ES6 syntax, you will have to spread the arguments, because the arguments do not exist inside of an arrow (lambda or anonymous) function.

((...vargs) => {
  try {
    console.log(arguments); // Not accessible
  } catch(e) {
    console.log(e);         // Expected to fail...
  }
  console.log(vargs);       // Variable (spread) arguments
})("a", "b", "c");
.as-console-wrapper { top: 0; max-height: 100% !important; }

Sign up to request clarification or add additional context in comments.

Comments

4

function f1() { console.log(Array.from(arguments)) }
f1(1, 2, 3)

function f2(...args) { console.log(args) }
f2(4, 5, 6)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.