gpt4 book ai didi

javascript - 在 javascript 中,如果数组、函数是对象原型(prototype),那么为什么 typeof 返回

转载 作者:行者123 更新时间:2023-12-03 11:22:42 25 4
gpt4 key购买 nike

在 javascript 中,如果数组、函数是对象原型(prototype),那么为什么 typeof 为保存数组的变量返回 object,而为保存函数引用的变量返回 Function。

最佳答案

the ECMAScript 3.0 specification (第 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

我在 1.0 规范中找不到任何对 typeof 的引用,虽然维基百科引用了 2.0 规范,但我在网上找不到它。

同时georg提出了一种可爱的观点,似乎 typeof 一元运算符在最初发布后的某个时候被添加到 JavaScript 中(因此必须对此进行一些思考)。

也就是说,有很多方法可以解决这些限制。只需谷歌“fixing typeof”...就有大​​约 4170 万个结果。 :)

关于javascript - 在 javascript 中,如果数组、函数是对象原型(prototype),那么为什么 typeof 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27037625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com