By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,738 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,738 IT Pros & Developers. It's quick & easy.

How to check typeof arguments?

P: n/a
I have this generic function (JavaScript newbie here, so don't think I am going to impress
you):

function blah()
{
var container = '';
for(var i = 0; i < arguments.length; i++)
{
container += arguments[i] + '\n';
}
alert(container);
}

Now, if I call this like:

blah('Me', 'Myself', 'I');

I get:

ALERT (you know, the alert dialog)
Me
Myself
I

However, if I call it with an array:

var arr_obj = new Array('Me', 'Myself', 'I');
blah(arr_obj);

I get:

ALERT
Me,Myself,I

I tried checking the type of the arguments object but no matter what is passed, arguments
always has a type of 'object'.

Is there something I am missing as to how to check whether or not I passed it an array or
a normal comma-separated string of values?

Also, forgive me if I am missing some fundamental thing here. Thanks!

-Lost
Jan 27 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
VK
I tried checking the type of the arguments object
but no matter what is passed, arguments always has a type of 'object'.
arguments object is - but not arguments passed to function.

function f() {
alert(typeof arguments[0]);
}

f('foobar'); // alerts 'string'
Is there something I am missing
when you do
var something+= arguments[0] + '\n';
then you call toString method for arguments[0]. If you have passed an
array reference as argument, toString for array object is called. And
for arrays toString returns comma-separated string of values.
as to how to check whether or not I passed it an array or
a normal comma-separated string of values?
if (typeof arguments[i] == 'string') {
// string argument
}
else if (arguments[i] instanceof Array) {
// array argument
}

Please not that you cannot uniform the check by using instanceof in
both cases. Implicit and explicit string constructors in JavaScript
are two separate entities, so say
(arguments[i] instanceof String) will be true only for explicit string
objects created over new String("something") - something you very
rarely want to do.

Jan 27 '07 #2

P: n/a
"-Lost" <sp****************@REMOVEMEcomcast.netwrote in
news:wo******************************@comcast.com:
I have this generic function (JavaScript newbie here, so don't think I
am going to impress you):

function blah()
{
var container = '';
for(var i = 0; i < arguments.length; i++)
{
container += arguments[i] + '\n';
}
alert(container);
}

Now, if I call this like:

blah('Me', 'Myself', 'I');

I get:

ALERT (you know, the alert dialog)
Me
Myself
I

However, if I call it with an array:

var arr_obj = new Array('Me', 'Myself', 'I');
blah(arr_obj);

I get:

ALERT
Me,Myself,I

I tried checking the type of the arguments object but no matter what
is passed, arguments always has a type of 'object'.

Is there something I am missing as to how to check whether or not I
passed it an array or a normal comma-separated string of values?

Also, forgive me if I am missing some fundamental thing here. Thanks!
See this tutorial for typeof and dltypeof:
http://www.webreference.com/dhtml/column68/
Jan 27 '07 #3

P: n/a
-Lost wrote:
I have this generic function (JavaScript newbie here, so don't think I am going to impress
you):
[...]
I tried checking the type of the arguments object but no matter what is passed, arguments
always has a type of 'object'.

Is there something I am missing as to how to check whether or not I passed it an array or
a normal comma-separated string of values?
How the typeof operator works is specified in section 11.4.3 of the
ECMAScript Language specification. Depending on the expression
provided, it returns one of:

Type Result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Object (native doesn’t
implement [[Call]]) "object"
Object (native and
implements [[Call]]) "function"
Object (host) Implementation-dependent
The only tricky part is that objects are split into plain objects and
function objects.

A javascript Array is an object constructed using the built-in Array
function object as a constructor, therefore typeof Array returns
'function'. A function's arguments object isn't a function, so typeof
arguments returns 'object'.

If you want to know more about an object, you can try the constructor
property but that can be inconsistent across browsers. It may be better
to use instanceOf.

var x = [];
alert(typeof x) // object
alert(x instanceof Array); // true
To determine whether arguments passed to a function are strings, numbers
or arrays, do something like:

function foo () {
var arg, args = arguments;
var len = args.length;

for (var i=0; i<len; i++){
arg = args[i];

if (typeof arg == 'string' || typeof arg == 'number'){
alert('arg ' + i + ' is a string or number');
} else if (arg instanceof Array){
alert('arg ' + i + ' is an Array');
} else if (arg instanceof Function){
alert('arg ' + i + ' is a Function');
}
}
}

foo( [], 6, 'blah', function(){} );
--
Rob
Jan 28 '07 #4

P: n/a
Thanks VK, Jim Land, and RobG! Great information from all three.

Now to absorb it all...

-Lost
Jan 28 '07 #5

P: n/a
dd
On Jan 27, 7:33 pm, "-Lost" <spam_ninjaREMOV...@REMOVEMEcomcast.net>
wrote:

Here's a version of blah that would do what you expect:

function blah() {
var ar=arguments.length==1?arguments.split(","):argume nts;
var container = '';
for(var i = 0; i < ar.length; i++)
{
container += ar[i] + '\n';
}
alert(container);
}

Jan 28 '07 #6

P: n/a


On Jan 28, 6:54 pm, "dd" <dd4...@gmail.comwrote:
On Jan 27, 7:33 pm, "-Lost" <spam_ninjaREMOV...@REMOVEMEcomcast.net>
wrote:

Here's a version of blah that would do what you expect:
Please don't top-post here, reply below trimmed quotes.
>
function blah() {
var ar=arguments.length==1?arguments.split(","):argume nts;
That will fail - the arguments object is not a string, it doesn't have
a split method. In most browsers, arguments is just an object with an
array-like length property and zero-indexed numeric property names.
In some browsers it is an Array object.
--
Rob

Jan 28 '07 #7

P: n/a
"RobG" <rg***@iinet.net.auwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
>

On Jan 28, 6:54 pm, "dd" <dd4...@gmail.comwrote:
>On Jan 27, 7:33 pm, "-Lost" <spam_ninjaREMOV...@REMOVEMEcomcast.net>
wrote:

Here's a version of blah that would do what you expect:

Please don't top-post here, reply below trimmed quotes.
>>
function blah() {
var ar=arguments.length==1?arguments.split(","):argume nts;

That will fail - the arguments object is not a string, it doesn't have
a split method. In most browsers, arguments is just an object with an
array-like length property and zero-indexed numeric property names.
In some browsers it is an Array object.
--
Rob
This *would* work though:

function blah()
{
var arg_obj = String(arguments[0]);
var arg = (arguments.length == 1) ? arg_obj.split(',') : arguments;
var container = '';
for(var i = 0; i < arg.length; i++)
{
container += arg[i] + '\n';
}
alert(container);
}

I wonder though, should I explicitly handle the scenarios? That is, when arguments[0] is
an object (possible array), should I test for it then convert it to a string? Otherwise,
leave arguments[0] alone?

-Lost
Jan 28 '07 #8

P: n/a
RobG wrote:
<snip>
... . In most browsers, arguments is just an object with an
array-like length property and zero-indexed numeric property
names. In some browsers it is an Array object.
The specification asserts that the - arguments - object should have the
original - Object.prototype - as its prototype, so it should not inherit
any methods or properties of arrays, and if it does that is an
implementation bug.

Richard.

Jan 28 '07 #9

P: n/a


On Jan 29, 7:14 am, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
RobG wrote:<snip>
... . In most browsers, arguments is just an object with an
array-like length property and zero-indexed numeric property
names. In some browsers it is an Array object.

The specification asserts that the - arguments - object should have the
original - Object.prototype - as its prototype, so it should not inherit
any methods or properties of arrays, and if it does that is an
implementation bug.
In Opera, the arguments object is an Array as indicated by the
following test:

function foo(){ alert(arguments instanceof Array);}
foo(); // Alerts "true" in Opera, "false" in other browsers

They probably consider it a feature rather than a bug. :-) As far
as I can tell, Waldemar Horwat[1] of Netscape suggested that arguments
be an Array in JavaScript 2.0 back in 2003.
1. <URL: http://www.mozilla.org/js/language/js20/core/
functions.html#arguments-array >

--
Rob

Jan 28 '07 #10

P: n/a
"RobG" <rg***@iinet.net.auwrote in message
news:11*********************@v33g2000cwv.googlegro ups.com...
On Jan 29, 7:14 am, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
>RobG wrote:<snip>
... . In most browsers, arguments is just an object with an
array-like length property and zero-indexed numeric property
names. In some browsers it is an Array object.

The specification asserts that the - arguments - object should have the
original - Object.prototype - as its prototype, so it should not inherit
any methods or properties of arrays, and if it does that is an
implementation bug.

In Opera, the arguments object is an Array as indicated by the
following test:

function foo(){ alert(arguments instanceof Array);}
foo(); // Alerts "true" in Opera, "false" in other browsers

They probably consider it a feature rather than a bug. :-) As far
as I can tell, Waldemar Horwat[1] of Netscape suggested that arguments
be an Array in JavaScript 2.0 back in 2003.
1. <URL: http://www.mozilla.org/js/language/js20/core/
functions.html#arguments-array >
Baring in mind I know very little at this point, I would also imagine that Opera considers
it a feature as opposed to a bug.

The fact that Opera supports a tremendous amount of DOM features as exhibited here:

http://www.w3.org/2003/02/06-dom-support.html

....makes me think they are keenly interested in supporting the standards, regardless. As
much as I love Opera I cannot abandon Firefox. Don't ask me why.

However, my recent musings (and the very informative responses I received) make me think
that arguments should either be a String or an Array based solely upon what was given to
said function. I realize of course that arguments is an object, as it has properties, but
still think it could be a little friendlier in its implementation.

That may just be a(n uninformed) newbie point of view. *shrugs*

-Lost
Jan 29 '07 #11

P: n/a
RobG wrote:
On Jan 29, 7:14 am, Richard Cornford wrote:
>RobG wrote:<snip>
>>... . In most browsers, arguments is just an object with an
array-like length property and zero-indexed numeric property
names. In some browsers it is an Array object.

The specification asserts that the - arguments - object should
have the original - Object.prototype - as its prototype, so it
should not inherit any methods or properties of arrays, and if
it does that is an implementation bug.

In Opera, the arguments object is an Array as indicated by the
following test:

function foo(){ alert(arguments instanceof Array);}
foo(); // Alerts "true" in Opera, "false" in other browsers
I know, it is a bug.
They probably consider it a feature rather than a bug. :-)
They would be wrong if they did. If they claim to be implementing ECMA
262 3rd Ed. then the prototype if the - arguments - object is specified
and doing anything other is a bug. Granted it is not a particularly
important bug as with everyone expecting the - arguments - object not to
be an array they are unlikely to treat it as such.
As far as I can tell, Waldemar Horwat[1] of Netscape suggested
that arguments be an Array in JavaScript 2.0 back in 2003.
It is a pity that next generation of javascript looks like an attempt to
make it more Java-like. It would probably be a better idea to not to go
in that direction and instead fix some of the ambiguities in the existing
specification. Making the - arguments - object into an array object (or
at least an array-like, as there is a specified linkage between then
'array index' properties of the - array - object and the formal parameter
properties of the Variable object) may be considered such an improvement.

Richard.

Jan 29 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.