简体   繁体   English

在JavaScript中,如果数组,函数是对象原型,那么为什么typeof返回

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

在javascript中,如果数组是函数,则函数是对象原型,那么为什么typeof为包含数组的变量返回对象,而对于包含对函数的引用的变量返回Function。

Per the ECMAScript 3.0 specification (Pages 46 - 47): 根据ECMAScript 3.0规范 (第46-47页):

11.4.3 The typeof Operator 11.4.3 typeof运算符

The production UnaryExpression : typeof UnaryExpression is evaluated as follows: 生产UnaryExpression: typeof UnaryExpression的评估如下:

  1. Evaluate UnaryExpression. 评估UnaryExpression。
  2. If Type(Result(1)) is not Reference, go to step 4. 如果Type(Result(1))不是Reference,请转到步骤4。
  3. If GetBase(Result(1)) is null , return "undefined" . 如果GetBase(Result(1))为null ,则返回“ undefined”
  4. Call GetValue(Result(1)). 调用GetValue(Result(1))。
  5. Return a string determined by Type(Result(4)) according to the following table: 根据下表返回由Type(Result(4))确定的字符串:
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. 我在1.0规范中找不到对typeof任何引用,而Wikipedia在引用2.0规范时,却无法在线找到它。

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). 尽管georg的观点很可爱,但看起来似乎是在最初发行后的某个时间将typeof一元运算符添加到了JavaScript中(因此必须加以考虑)。

That said, there are many ways around the limitations. 也就是说,有很多方法可以解决这些限制。 Just google " fixing typeof "... there's around 41.7 million results. 只是谷歌“ fixing typeof ” ...有大约4170万结果。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM