Reputation: 341
in javascript if arrays, functions are objects prototypes then why typeof returns object for a variable that holds an array and it returns Function for a variable that holds a reference to a function.
Upvotes: 0
Views: 81
Reputation: 25081
Per the ECMAScript 3.0 specification (Pages 46 - 47):
11.4.3 The typeof Operator
The production UnaryExpression : typeof UnaryExpression is evaluated as follows:
- Evaluate UnaryExpression.
- If Type(Result(1)) is not Reference, go to step 4.
- If GetBase(Result(1)) is null, return "undefined".
- Call GetValue(Result(1)).
- Return a string determined by Type(Result(4)) according to the following table:
Type Result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
Object (native and doesn’t implement [[Call]]) "object"
Object (native and implements [[Call]]) "function"
Object (host) Implementation-dependent
I couldn't find any reference to typeof
in the 1.0 specification, and while wikipedia references a 2.0 specification, I couldn't find it online.
While georg makes a lovely sentiment, it would appear that the typeof
unary operator was added to JavaScript sometime after the initial release (so some thought had to have been put into it).
That said, there are many ways around the limitations. Just google "fixing typeof"... there's around 41.7 million results. :)
Upvotes: 1