![](/img/trans.png)
[英]Why is the string literal considered a primitive type in JavaScript?
[英]If an primitive type is considered an object then is it considered an array?
当我们像本段下面的示例那样检索原始类型的描述符时,我们指定一个 integer 作为对象(它也成为属性的值),我们也指定键为“0”。 现在因为我们指定了 integer 作为键,这是否意味着从 ES2015 开始,原始类型被视为数组?
var e = Object.getOwnPropertyDescriptor([50], 0);
'var e' 的值:
{
configurable: false,
enumerable: true,
value: 50,
writable: false
}
或者在下一个示例中,我们传入一个字符串值:
var e = Object.getOwnPropertyDescriptor("hello", 0);
这会产生一些不同的结果:
configurable: false
enumerable: true
value: "h"
writable: false
现在,如果我们传入一个字符串,它也被视为一个数组。 但似乎只有第一个字符被保留为值。 为什么也会发生这种情况?
当我们尝试获取原语的描述符时,它们首先根据规范转换为 object 形式:
20.1.2.8 Object.getOwnPropertyDescriptor (O, P)
当调用getOwnPropertyDescriptor function时,采取以下步骤:
- 让 obj 成为? 对象(O)。
- 让钥匙成为? ToPropertyKey(P)。
- 让 desc 成为? obj.[GetOwnProperty]。
- 返回 FromPropertyDescriptor(desc)。
因此,我们从Object.getOwnPropertyDescriptor
获得的任何信息都不会告诉我们有关原语本身的任何信息,它只会告诉我们有关从toObject步骤推出的 object 的属性的一些信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.