Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check whether an object is an arguments object in JavaScript?

Tags:

javascript

I'm in ES5 strict mode, so the solution

function isArguments(item) {
    return item.callee !== undefined;
}

unfortunately doesn't work.

like image 259
wwww Avatar asked Oct 05 '11 03:10

wwww


People also ask

What is an arguments object in JS?

The arguments object is a local variable available within all non-arrow functions. You can refer to a function's arguments inside that function by using its arguments object. It has entries for each argument the function was called with, with the first entry's index at 0 .

How do you check if an item is in an object JavaScript?

JavaScript provides you with three common ways to check if a property exists in an object: Use the hasOwnProperty() method. Use the in operator. Compare property with undefined .

How do you check if a variable is an object?

Use the typeof operator to verify that the variable type is object — typeof obj === 'object' . Verify the value is not null — obj !== null .

How do I know the type of an object?

Use the typeof operator to get the type of an object or variable in JavaScript. The typeof operator also returns the object type created with the "new" keyword. As you can see in the above example, the typeof operator returns different types for a literal string and a string object.


1 Answers

William's answer is right, but some explanations may be useful.

In ECMAScript 5, the only thing that characterizes Arguments objects is their internal [[Class]], as seen in §10.6 Arguments Object:

When CreateArgumentsObject is called the following steps are performed:

  • Let obj be the result of creating a new ECMAScript object.
  • Set the [[Class]] internal property of obj to "Arguments".
  • Return obj

[[Class]] is an internal property common to all objects, whose value is a String which classifies the object. This is explained in §8.6.2 Object Internal Properties and Methods:

The value of the [[Class]] internal property is defined by this specification for every kind of built-in object.

The value of a [[Class]] internal property is used internally to distinguish different kinds of objects.

Moreover, note that host objects won't be problematic:

The value of the [[Class]] internal property of a host object may be any String value except one of "Arguments", [...]

Therefore, to identify an Arguments object you only need to check its class.

You can do that using §15.2.4.2 Object.prototype.toString:

  • Let O be the result of calling ToObject passing the this value as the argument.
  • Let class be the value of the [[Class]] internal property of O.
  • Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".

Therefore, you can use Function.prototype.call to call that method with the this value set to the object you want to check. The returned string will be '[object Arguments]' if, and only if, it's an Arguments object.

Object.prototype.toString.call(obj) == '[object Arguments]'

Note that isn't completely foolproof, because the global Object could have been shadowed by a local one, or the global Object or its toString property could have been modified.

However, there is no better way:

Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).

like image 148
Oriol Avatar answered Oct 20 '22 00:10

Oriol