user3021621
user3021621

Reputation: 341

in javascript if arrays, functions are objects prototypes then why typeof returns

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

Answers (1)

pete
pete

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:

  1. Evaluate UnaryExpression.
  2. If Type(Result(1)) is not Reference, go to step 4.
  3. If GetBase(Result(1)) is null, return "undefined".
  4. Call GetValue(Result(1)).
  5. 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

Related Questions