为什么String(null)有效? [英] Why does String(null) work?
问题描述
null
和 undefined
没有 toString
或 valueOf
方法。 Afaik使用 String
调用其参数的 toString
方法(例如 String({})
=> [object Object]
)。
null
and undefined
don't have a toString
or valueOf
method. Afaik using String
calls the toString
method of its parameter (e.g. String({})
=> [object Object]
).
为什么 String(null)
或 String(undefined
然后工作?它不隐式地执行 Object.prototype.toString.call(null)
。因为它的计算结果为 [object Null]
。
Why do String(null)
or String(undefined
work then? It doesn't implicitly do Object.prototype.toString.call(null)
. because that evaluates to [object Null]
.
:来自ECMA-262 / 5th版规格(第48页)。这不会增加澄清,我会说:
[edit]: from the spec ECMA-262/5th edition (page 48). This doesn't add to clarification, I'd say:
/*
Table 13 — ToString Conversions
-------------------------------------------------------------------------
Argument Type | Result
-------------------------------------------------------------------------
Undefined | "undefined"
Null | "null"
Boolean | If the argument is true, then the result is "true".
... | ...
*/
推荐答案
在查看我以前的答案后,它似乎对我之前的答案进行了彻底的改革是必要的。我正在将它复杂化,就像简短的回答一样r是这些是标准规定的特殊情况。
After reviewing my previous answer, it seems a complete overhaul of my previous answer is necessary. I was way over complicating it, as the short answer is that these are standards-specified special cases.
规范 字符串()
(字符串
用作函数):
15.5.1.1字符串([value])
15.5.1.1 String ( [ value ] )
返回由ToString(value)计算的String值(不是String对象)。如果未提供value,则返回空的
String。
Returns a String value (not a String object) computed by ToString(value). If value is not supplied, the empty String "" is returned.
ToString
函数(内部存在,不存在于userland中)已定义如下(9.8):
The ToString
function (that exists internally, not in userland) is defined as follows (9.8):
抽象操作ToString根据表13将其参数转换为String类型的值
"The abstract operation ToString converts its argument to a value of type String according to Table 13"
Argument Type | Result
Null | "null"
Undefined | "undefined"
这意味着 String(null)
和字符串(未定义)
进入这个特殊的类型表,只返回值为null的字符串值
和undefined
。
This means that String(null)
and String(undefined)
go into this special table of types and just return the string values valued "null"
and "undefined"
.
user-land伪实现看起来像这样:
A user-land pseudo-implementation looks something like this:
function MyString(val) {
if (arguments.length === 0) {
return "";
} else if (typeof val === "undefined") {
return "undefined";
} else if (val === null) {
return "null";
} else if (typeof val === "boolean") {
return val ? "true" : "false";
} else if (typeof val === "number") {
// super complex rules
} else if (typeof val === "string") {
return val;
} else {
// return MyString(ToPrimitive(val, prefer string))
}
}
(注意,这个例子忽略了构造函数的情况( new MyString()
)并且它使用了用户区概念而不是比发动机陆地。)
(Note that this example ignores the constructor case (new MyString()
) and that it uses user-land concepts rather than engine-land.)
我有点匆匆忙忙地找到了一个示例实现(V8具体):
I got a bit carried away and found an example implementation (V8 to be specific):
string.js:
// Set the String function and constructor.
%SetCode($String, function(x) {
var value = %_ArgumentsLength() == 0 ? '' : TO_STRING_INLINE(x);
if (%_IsConstructCall()) {
%_SetValueOf(this, value);
} else {
return value;
}
});
macros.py:
macro TO_STRING_INLINE(arg) = (IS_STRING(%IS_VAR(arg)) ? arg : NonStringToString(arg));
runtime.js:
function NonStringToString(x) {
if (IS_NUMBER(x)) return %_NumberToString(x);
if (IS_BOOLEAN(x)) return x ? 'true' : 'false';
if (IS_UNDEFINED(x)) return 'undefined';
return (IS_NULL(x)) ? 'null' : %ToString(%DefaultString(x));
}
NonStringToString(基本上是我感兴趣的),幸运的是在伪JS-土地。正如您所看到的,确实存在null / true / false / undefined的特殊情况。
The NonStringToString (which is essentially what is of interest), is luckily defined in psuedo-JS-land. As you can see, there is indeed a special case for null/true/false/undefined.
这篇关于为什么String(null)有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!