有没有返回的JavaScript类型“数组”? [英] Has JavaScript typeof ever returned "array"?
问题描述
我已经看到这个成语出现在各种开源项目中:
I've seen this idiom show up in various open-source projects:
it instanceof Array || typeof it == "array"
我知道ECMAScript 5(或6)中的后半部分不可能是真的),但是/有没有一个实现可以从typeof返回数组?或者这是一个虚假的防御性编程案例?
I know the second half cannot be true in ECMAScript 5 (or 6), but is/was there ever an implementation that would return "array" from typeof? Or is this a case of bogus defensive programming?
推荐答案
不,至少从ECMAScript的第一个版本开始。
No, at least since the first version of ECMAScript.
ECMAScript 1 (1997年6月)
ECMAScript 1 (June 1997)
11.4.3 -
typeof
运算符
11.4.3 - The
typeof
operator
生产 UnaryExpression : typeof
UnaryExpression 是
评估如下:
The production UnaryExpression : typeof
UnaryExpression is
evaluated as follows:
- 评估 UnaryExpression 。
- 如果Type(Result(1))为Reference且GetBase(Result(1))为 null ,则返回
undefined
。 - 调用GetValue(结果(1))。
-
根据下表返回由Type(Result(3))确定的字符串:
- Evaluate UnaryExpression.
- If Type(Result(1)) is Reference and GetBase(Result(1)) is null, return
"undefined"
. - Call GetValue(Result(1)).
Return a string determined by Type(Result(3)) according to the following table:
- 未定义:
undefined
- Null:
object
- 布尔值:
boolean
- 数字:
数字
- 字符串:
string
- 对象(本机且未实现[[Call]]):
object
- 对象(本机和实现[[Call]]):
function
- 对象(主机):依赖于实现
- 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 2 (August 1998)
无变化。
ECMAScript 3 (1999年12月)
ECMAScript 3 (December 1999)
无相关变化。
11.4.3 -
typeof
运算符
11.4.3 - The
typeof
operator
生产 UnaryExpression : typeof
UnaryExpression 是
已评估如下:
The production UnaryExpression : typeof
UnaryExpression is
evaluated as follows:
- 评估 UnaryExpression 。
- 如果类型(结果) (1))不是参考,转到步骤4.
- 如果GetBase(Result(1)) null ,则返回
undefined
。 - 调用GetValue(Result(1))。
-
返回由Type确定的字符串(结果(4))根据下表:
- 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:
- 未定义:
undefined
- Null:
object
- 布尔值:
boolean
- 数字:
数字
- 字符串:
string
- 对象(本机且未实现[[Call]]):
object
- 对象(native和implements [[Call]]):
function
- 对象(主机):依赖于实现
- 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类型“数组”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!