我写了一个小framework for asynchronous resource loading (link) .在 Opera、Chrome、Firefox 等大多数现代浏览器中,它都按预期工作。
我在IE8下也试过了,但是代码不执行。 IE 在一个简单的格式良好的 [通过 eval() 检查] JSON 对象上抛出“对象不支持此属性或方法”。
我该如何解决这个问题或导致此错误的原因?
最佳答案
可能引发错误是因为 IE8 不支持 .indexOf()
.
引用:
indexOf was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all browsers. You can work around this by utilizing the following code at the beginning of your scripts. This will allow you to use indexOf when there is still no native support. This algorithm matches the one specified in ECMA-262, 5th edition, assuming TypeError and Math.abs have their original values.
使用 polyfill 代码:
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
"use strict";
if (this == null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) {
return -1;
}
var n = 0;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (n != n) { // shortcut for verifying if it's NaN
n = 0;
} else if (n != 0 && n != Infinity && n != -Infinity) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) {
return -1;
}
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
}
}
关于javascript - 简单 JSON 对象抛出 "Object doesn' t 支持此属性或方法”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21546249/