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/