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

Can we know if an object and a function "arguments" are of same type?

P: n/a
I wanna check if an object is the *arguments* object of a function, is
that possible?

When we do:

typeof(arguments)

it always returns "object", doesn't seem to be helpful.

Oct 15 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
VK
runsun pan wrote:
I wanna check if an object is the *arguments* object of a function, is
that possible?
In what context to you need to make this check?

Within the function which this [arguments] object appertains to you can
check arguments specific properties (callee, caller) for existence (but
what practical reason would be for that?)

If you are thinking of apply method like

function f1() {
f2.apply(null, arguments);
}

function f2(args) {
// what kind of args is that?
}

then I don't see anything you could do because arguments gets
refactored before sent to f2.

Oct 15 '06 #2

P: n/a

runsun pan wrote:
I wanna check if an object is the *arguments* object of a function, is
that possible?

When we do:

typeof(arguments)

it always returns "object", doesn't seem to be helpful.
Objects are only ever equal to themselves, so:

function blah(){
if (someObj == arguments) {
/* someObj is the function's arguments object */
}
}

but that seems rather pointless. Why would you ever need to test if
someObj was the arguments object?
--
Rob

Oct 16 '06 #3

P: n/a
VK wrote:
In what context to you need to make this check?
I am trying to write an inspection function such that I can do this:

dir(object)

and it will show me what the object content is. Inside that function
dir() a type check is carried out to determine what to do depending on
different types. For example, if it's an array, it will go check items,
and do a recursive dir if needed. Upon output it will be wrapped with
"[" and "]".

Certainly only arrays need to do this. Other types need different
treatments.

I am just trying to cover as many types as possible.

Oct 16 '06 #4

P: n/a
runsun pan wrote:
VK wrote:
In what context to you need to make this check?

I am trying to write an inspection function such that I can do this:

dir(object)

and it will show me what the object content is. Inside that function
dir() a type check is carried out to determine what to do depending on
different types. For example, if it's an array, it will go check items,
and do a recursive dir if needed. Upon output it will be wrapped with
"[" and "]".

Certainly only arrays need to do this. Other types need different
treatments.

I am just trying to cover as many types as possible.
Then (as suggested by VK) look at the object's properties. For
built-in objects like Math, Array, Object, etc. you can pick them from
their constructor property, but for other objects it is a matter of
deduction.

The arguments object has:

- a constructor of Object
- a numeric length
- a callee property whose typeof is function
- a callee.caller property whose typeof is object.

Probably the callee property is sufficient, since no other object
should have it. But if you think you may run across a circumstance
where someone may have added a callee property to some other type of
object, then test for other properties.

if (
typeof someObj == 'object'
&& typeof someObj.length == 'number'
&& typeof someObj.callee == 'function'
&& typeof someObj.callee.caller == 'object'
){
/* someObj is an arguments object */
}

--
Rob

Oct 16 '06 #5

P: n/a
RobG wrote:
[..]
The arguments object has:

- a constructor of Object
- a numeric length
- a callee property whose typeof is function
- a callee.caller property whose typeof is object.
Ooops, callee.caller might also be a function.

Probably the callee property is sufficient, since no other object
should have it. But if you think you may run across a circumstance
where someone may have added a callee property to some other type of
object, then test for other properties.

if (
typeof someObj == 'object'
&& typeof someObj.length == 'number'
&& typeof someObj.callee == 'function'
&& typeof someObj.callee.caller == 'object'
That should be:

&& (typeof someObj.callee.caller == 'object'
|| typeof someObj.callee.caller == 'function')
){
/* someObj is (probably) an arguments object */
}

--
Rob

Oct 16 '06 #6

P: n/a
RobG wrote:

[snip]

[Properties of the arguments object]
- a constructor of Object
Not in Opera: the arguments object is an array.

[snip]
- a callee.caller property whose typeof is object.
Where implemented (and I should think it would only be function). Again,
this would fail in Opera.

[snip]

Mike
Oct 16 '06 #7

P: n/a
Thx guys. It's all very helpful.

Oct 16 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.