2

In javascript you can get the arguments of a function as an array-like object via the keyword arguments. I was wondering if there is a way to check if an object originated from the arguments keyword.

(What i basically wanna do is this: if(myvar.constructor.name === "Arguments") {(...)}, but the arguments object uses the default Object class, which is why this isn't possible.)

(What leads me to believe doing this is possible is the browser labelling an arguments object as "Arguments" when logged to the console)

1
  • "What leads me to believe doing this is possible is the browser labelling an arguments object as "Arguments" when logged to the console" - actually, the console (part of the debugging tools!) can do lots of stuff that ordinary scripts cannot, so this is not a good hint. Commented Apr 17, 2020 at 21:33

2 Answers 2

2

You can cast it to a string and compare it against [object Arguments]:

const isArguments = param => param.toString() === '[object Arguments]';
function foo() {
  console.log(isArguments(arguments));
  console.log(isArguments({}));
}

foo();

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

Comments

2

The only way to detect them is to use the Obect.prototype.toString "class" check:

if (Object.prototype.toString.call(myVar) == "[object Arguments]")

Since ES6, this can be fooled with a [Symbol.toStringTag] property though.

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.