gpt4 book ai didi

javascript - 为什么 JavaScript 中的 `Object.length === 1` 是浏览器的一部分?

转载 作者:搜寻专家 更新时间:2023-11-01 05:04:45 26 4
gpt4 key购买 nike

我正在对 Angular 中的某些问题进行故障排除,最终检查 Object.length。结果为 1。它的字符串表示不显示任何参数。我知道使用了第一个参数。

var obj = new Object({ a : 27 });

Object's constructor function lists the argument as optional.

document.write(Object.length);

那么为什么/如何计算?

最佳答案

函数的长度是声明参数的数量,不包括任何剩余参数

来自 MDN:

length is a property of a function object, and indicates how many arguments the function expects, i.e. the number of formal parameters. This number does not include the rest parameter.

There's exactly one for the Object constructor ,可选的包装值。

此外,在 Object 函数的特定情况下,the ES spec explicitly mandates it should have a length of 1 :

Besides the internal properties and the length property (whose value is 1)

(它似乎对所有采用 rest 参数的函数都这样做,比如 ArrayfromCharCode)

请注意,浏览器中 native 函数的 ( implementation dependent) 字符串表示不一定会显示形式参数。

关于javascript - 为什么 JavaScript 中的 `Object.length === 1` 是浏览器的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29301822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com