const myError = new Error("bug");
myError.propertyIsEnumerable("message") //false
myError.propertyIsEnumerable("stack") //false
这样做的含义是,当您for循环这种对象时,您将不会获取这些字段,这些字段我觉得很奇怪。
其背后的原因是什么?
答案 0 :(得分:0)
我能想到的原因是保持Object.keys
的行为。如果您看到初始规格,则错误部分不是很详细。错误规范为
This specification specifies the last possible moment an error occurs. A given implementation may generate errors
sooner (e.g. at compile-time). Doing so may cause differences in behavior among implementations. Notably, if runtime
errors become catchable in future versions, a given error would not be catchable if an implementation generates the
error at compile-time rather than runtime.
An ECMAScript compiler should detect errors at compile time in all code presented to it, even code that detailed
analysis might prove to be “dead” (never executed). A programmer should not rely on the trick of placing code within
an if (false) statement, for example, to try to suppress compile-time error detection.
In general, if a compiler can prove that a construct cannot execute without error under any circumstances, then it may
issue a compile-time error even though the construct might neverbe executed at all.
看到第一个规格