简体   繁体   English

创建的Number.isInteger(x)无法在IE中工作

[英]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.

相关问题 与 Number.isInteger() 苦苦挣扎 - struggling with Number.isInteger() Number.isInteger(this) 在 Number.prototype 方法中不起作用 - Number.isInteger(this) doesn't work in Number.prototype method 我还能用什么来代替 Number.isInteger? - What else can i use instead of Number.isInteger? 如何在JavaScript中修复Number.isInteger()函数 - How to fix Number.isInteger() function in JavaScript if.IsInteger()在if else语句中不起作用 - Number.IsInteger() in if else statement not working 我需要检查一个数字是否不是小数,我正在尝试使用 Number.isInteger() 但不起作用 - I need to check if a number is not a decimal, I'm trying with Number.isInteger() but doesn't work 在使用 Number.isInteger 检查 object 属性是 integer 后,object 怎么可能为 null/undefined? - How can object be null/undefined after checking that object property is an integer using Number.isInteger? 使用 Number.isInteger() 方法得到错误的结果,javascript - Getting wrong result with Number.isInteger() method, javascript 在以下Javascript数组方案中,parseInt()和Number.isInteger()之间的功效差异? - Difference in efficacy between parseInt() and Number.isInteger() in following Javascript array scenario? 使用 isInteger 检查字符(作为字符串)是否为数字不起作用 - Checking if a character (as a string) is digit with isInteger does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM