gpt4 book ai didi

JavaScript 闭包 __proto__

转载 作者:行者123 更新时间:2023-12-01 03:15:03 24 4
gpt4 key购买 nike

function makeAdder(x) {
return function(y) {
return x + y;
};
}
var add5 = makeAdder(5);
add5(2); //7;
add5(2).__proto__; //0`

这是有道理的。但是,add5(2) 是对象吗?为什么有0?

最佳答案

我不知道您在哪个环境中运行代码,但这就是当您尝试在数字值上访问__proto__时发生的情况:该值是暂时转换为数字对象(即相当于调用new Number(7))。该对象的原型(prototype)当然是 Number.prototype

The spec says :

The Number prototype is itself a Number object; it has a [[NumberData]] internal slot with the value +0.

我只能假设您使用的环境检测到 add5(2).__proto__ 是一个数字对象并调用其 valueOf 方法,然后该方法返回 0:

console.log(Number.prototype.valueOf());

关于JavaScript 闭包 __proto__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45597139/

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