gpt4 book ai didi

javascript - JavaScript typeof 曾经返回过 "array"吗?

转载 作者:数据小太阳 更新时间:2023-10-29 04:15:47 24 4
gpt4 key购买 nike

我在各种开源项目中看到过这个习语:

it instanceof Array || typeof it == "array"

我知道后半部分在 ECMAScript 5(或 6)中不可能为真,但是是否有从 typeof 返回“array”的实现?或者这是一个伪造的防御性编程案例?

最佳答案

不,至少从 ECMAScript 的第一个版本开始是这样。

ECMAScript 1 (1997 年 6 月)

11.4.3 - The typeof operator

The production UnaryExpression : typeof UnaryExpression is evaluated as follows:

  1. Evaluate UnaryExpression.
  2. If Type(Result(1)) is Reference and GetBase(Result(1)) is null, return "undefined".
  3. Call GetValue(Result(1)).
  4. Return a string determined by Type(Result(3)) according to the following table:

    • 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

ECMAScript 2 (1998 年 8 月)

没有变化。

ECMAScript 3 (1999 年 12 月)

无相关变化。

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:

    • 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

ECMAScript 4

此规范已被放弃。

关于javascript - JavaScript typeof 曾经返回过 "array"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30264923/

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