ECMAScript 中的 'standard object' 和 'ordinary object' 有什么区别?
What is the difference between a 'standard object' and an 'ordinary object' in ECMAScript?
在 ECMAScript 2021 语言规范的术语和定义部分中,ordinary object 定义为:
object that has the default behaviour for the essential internal
methods that must be supported by all objects
一个standard object定义为:
object whose semantics are defined by this specification
看完这两个定义后,我立即问自己,“所有对象必须支持的基本内部方法的默认行为不是也在本规范中定义了吗?”
我已经尝试搜索这两个术语的规范,但是 'ordinary object' 有 100 多个匹配项,'standard object' 只有少数参考资料,它们没有提供额外的上下文这些术语之间的区别对我来说很清楚。我还尝试了 Google 搜索,none 的结果似乎与我的问题相关。
普通对象和标准对象有什么区别?区分这两种类型的对象很有用的场景示例是什么?
const user = { firstname: "Jonas" };
那是一个普通对象(因为它不是奇异),但它不是标准对象,因为语义是由我定义的,而不是由规范定义的。但是,它的行为是指定的(例如 user.firstname
将计算为 "Jonas"
)。
是的,每个普通对象也是标准对象。
术语普通对象与奇异对象形成对比,后者确实具有内部对象方法的非默认实现。 standard exotic objects 的示例是代理、数组和绑定函数。
术语标准对象(以前也称为“native object", which led to a lot of confusion) is contrasted with a host object or implementation-defined object, which are specified by a different specification (e.g. HTML5) or implemented in the engine without a formal specification. These can be functions (that are otherwise ordinary) with a host-defined call behaviour (e.g. console.log
, setTimeout
etc) or totally exotic objects (document.all
, nodelists, etc) - see web APIs,用于概述浏览器定义的对象。其他环境将提供其他主机定义的对象。
存在 受主机实现或非 ECMAScript 标准控制的普通对象(具有标准的默认行为),我猜可以归类为其中之一。
仅供参考,与此术语讨论相关的最后一个区别是内置对象(also builtins)和用户-定义的对象。前者由启动时的实现创建,提供用户代码可以与之交互的环境。后者由用户代码在运行时构建。不幸的是,当外来对象由本机(非用户)代码构建时,它确实成为一个灰色区域,这些有时也被称为“内置”。
另见 What is the difference between native objects and host objects? and In ECMAScript, how are some of native objects also built-in?。
在 ECMAScript 2021 语言规范的术语和定义部分中,ordinary object 定义为:
object that has the default behaviour for the essential internal methods that must be supported by all objects
一个standard object定义为:
object whose semantics are defined by this specification
看完这两个定义后,我立即问自己,“所有对象必须支持的基本内部方法的默认行为不是也在本规范中定义了吗?”
我已经尝试搜索这两个术语的规范,但是 'ordinary object' 有 100 多个匹配项,'standard object' 只有少数参考资料,它们没有提供额外的上下文这些术语之间的区别对我来说很清楚。我还尝试了 Google 搜索,none 的结果似乎与我的问题相关。
普通对象和标准对象有什么区别?区分这两种类型的对象很有用的场景示例是什么?
const user = { firstname: "Jonas" };
那是一个普通对象(因为它不是奇异),但它不是标准对象,因为语义是由我定义的,而不是由规范定义的。但是,它的行为是指定的(例如 user.firstname
将计算为 "Jonas"
)。
是的,每个普通对象也是标准对象。
术语普通对象与奇异对象形成对比,后者确实具有内部对象方法的非默认实现。 standard exotic objects 的示例是代理、数组和绑定函数。
术语标准对象(以前也称为“native object", which led to a lot of confusion) is contrasted with a host object or implementation-defined object, which are specified by a different specification (e.g. HTML5) or implemented in the engine without a formal specification. These can be functions (that are otherwise ordinary) with a host-defined call behaviour (e.g. console.log
, setTimeout
etc) or totally exotic objects (document.all
, nodelists, etc) - see web APIs,用于概述浏览器定义的对象。其他环境将提供其他主机定义的对象。
存在 受主机实现或非 ECMAScript 标准控制的普通对象(具有标准的默认行为),我猜可以归类为其中之一。
仅供参考,与此术语讨论相关的最后一个区别是内置对象(also builtins)和用户-定义的对象。前者由启动时的实现创建,提供用户代码可以与之交互的环境。后者由用户代码在运行时构建。不幸的是,当外来对象由本机(非用户)代码构建时,它确实成为一个灰色区域,这些有时也被称为“内置”。
另见 What is the difference between native objects and host objects? and In ECMAScript, how are some of native objects also built-in?。