runsun said:
VK said:
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 */
}