gpt4 book ai didi

Javascript:数字的原型(prototype)

转载 作者:行者123 更新时间:2023-11-29 10:25:31 25 4
gpt4 key购买 nike

var x= 1;  
Number.prototype.test = function () { return this };
x.test() === x.test() // false

为什么 === 测试返回 false?

最佳答案

因为 this 将是一个 Number 对象,而不是原始原始数字值,比较两个同样创建的对象将始终返回 false:

{"test":"Hello"} === {"test":"Hello"} // false

// Check the typeof your vars
var x= 1;
Number.prototype.test = function () { return this };
x.test() === x.test() // false
alert("x is a "+typeof(x)+", x.test() is an "+typeof(x.test()));

如果您正在寻找修复方法,请将 this 转换为数字

var x= 1;  
Number.prototype.test = function () { return +this };
x.test() === x.test() // TRUE!
alert("x is a "+typeof(x)+", x.test() is also a "+typeof(x.test()));

关于Javascript:数字的原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2400689/

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