繁体   English   中英

Object.keys在IE11中工作的解决方法是什么?

[英]What is the workaround for Object.keys to work in IE11?

我正在尝试使用在IE8的Object.keys上找到的以下功能

 Object.keys = Object.keys || (function() { var hasOwnProperty = Object.prototype.hasOwnProperty, hasDontEnumBug = !{ toString: null }.propertyIsEnumerable("toString"), DontEnums = [ 'toString', 'toLocaleString', 'valueOf', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'constructor' ], DontEnumsLength = DontEnums.length; return function(o) { if (typeof o != "object" && typeof o != "function" || o === null) throw new TypeError("Object.keys called on a non-object"); var result = []; for (var name in o) { if (hasOwnProperty.call(o, name)) result.push(name); } if (hasDontEnumBug) { for (var i = 0; i < DontEnumsLength; i++) { if (hasOwnProperty.call(o, DontEnums[i])) result.push(DontEnums[i]); } } return result; }; })(); 

这是我尝试实现的方法:

 var attributeCodeTextLength = Object.keys(item.versionRoomTypeAttributeList[0].attributeCode).length; var attributeLimit = 7; if (Object.keys(item.versionRoomTypeAttributeList[0].attributeCode).length > 2) { attributeLimit = 5; if (Object.keys(item.versionRoomTypeAttributeList[0].attributeCode).length > 3) { attributeLimit = 4; } } 

但是我一直收到以下错误:

TypeError: Object.keys: argument is not an Object

这是我发现可以使其工作的Polyfill

 Object.keys = function(obj) { var keys = []; for (var i in obj) { if (obj.hasOwnProperty(i)) { keys.push(i); } } return keys; }; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM