[英]Number.isInteger(x) which is created can not work in IE
Number.prototype.isInteger = Number.prototype.isInteger || function(x) { return (x ^ 0) === x; } console.log(Number.isInteger(1));
will throw error in IE10 browser 将在IE10浏览器中抛出错误
Apparently, IE treats DOM objects and Javascript objects separately, and you can't extend the DOM objects using Object.prototype. 显然,IE分别处理DOM对象和Javascript对象,并且您无法使用Object.prototype扩展DOM对象。
IE doesn't let you use a prototype that is not native.. IE不允许你使用非原生的原型..
You'll have to make a separate function (global if you want) as 你必须创建一个单独的函数(全局,如果你想)
function isInteger(num) {
return (num ^ 0) === num;
}
console.log(isInteger(1));
Notwithstanding possible issues with adding to native prototypes in MSIE, your function body is inappropriate for a method added to Number.prototype
. 尽管在MSIE中添加到原生原型可能存在问题,但您的函数体不适合添加到Number.prototype
的方法。
Methods on the prototype are called on instances of the type, and the instance is passed as this
(and will always be an object, not a primitive). 原型上的方法在类型的实例上调用, 实例作为this
传递(并且将始终是对象,而不是基元)。
Therefore a more correct implementation would be: 因此,更正确的实现将是:
Number.prototype.isInteger = function() {
return (this ^ 0) === +this;
}
with usage: 用法:
(1).isInteger();
If you wanted to use Number.isInteger(n)
instead, you would have had to add your function directly to the Number
object, not its prototype. 如果你想使用Number.isInteger(n)
,你必须直接将你的函数添加到Number
对象,而不是它的原型。 There's a rigorous shim for this on the MDN page for this function . 这个功能的MDN页面上有一个严格的垫片。
Create a polyfill Number.isInteger
创建Number.isInteger
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
This should solve the issue related to IE. 这应该解决与IE相关的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.