gpt4 book ai didi

javascript - IE8 对象当对象包含空值时出现预期错误

转载 作者:行者123 更新时间:2023-12-03 10:41:09 27 4
gpt4 key购买 nike

我使用了一个小的多边形填充,以使 Object.key 可用于 IE8。当我的对象包含所有值时,一切似乎都工作正常。但是,当对象中有空值时,我会收到有关预期对象的错误。我不确定为什么 null 很重要。我的代码如下:

  var data = [{
x: Date.UTC(2014, 8, 1),
y: 730,
pf: ["001", "002"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}, {
x: Date.UTC(2014, 9, 1),
y: 742,
pf: ["001", "002"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}, {
x: Date.UTC(2014, 10, 1),
y: 746,
pf: ["001", "002"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}, {
x: Date.UTC(2014, 11, 1),
y: null
}, {
x: Date.UTC(2015, 0, 1),
y: 737,
pf: ["001", "002"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}, {
x: Date.UTC(2015, 1, 1),
y: 751,
pf: ["001", "002"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}, {
x: Date.UTC(2015, 2, 1),
y: 765,
pf: ["001", "002"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}, {
x: Date.UTC(2015, 3, 1),
y: 766,
pf: ["001", "002"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}, {
x: Date.UTC(2015, 4, 1),
y: 757,
pf: ["001", "002"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}, {
x: Date.UTC(2015, 5, 1),
y: 750,
pf: ["001", "002"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}, {
x: Date.UTC(2015, 6, 1),
y: 748,
pf: ["001", "002"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}, {
x: Date.UTC(2015, 7, 1),
y: 723,
pf: ["001", "003"],
nf: ["001", "002"],
t: ["001", "002"],
im:["001"]
}]


var pfMap = {
"001": {
"sd": "HI",
"ld": "By"
},
"002": {
"sd": "HI",
"ld": "By"
}
}

var nfMap = {
"001": {
"sd": "HI",
"ld": "By"
}
}

var tMap = {
"001": "Hi",
"002": "by"
}

var imMap={
"001":"HI"
}

// Shim added to aid in IE8

if (!Object.keys) {
Object.keys = (function() {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;

return function(obj) {
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}

var result = [], prop, i;

for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}

if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
}());
}

//recursion

function traverseData(data, elementName) {
var keys = Object.keys(data);
for (var i = 0; i < keys.length; ++i) {
if (Object.prototype.toString.call(data[keys[i]]) == "[object Object]" || Object.prototype.toString.call(data[keys[i]]) == "[object Array]") {
traverseData(data[keys[i]], keys[i]);
} else {
if (elementName != null && window[elementName + "Map"]) {
data[keys[i]] = window[elementName + "Map"][data[keys[i]]];
}

}

}

}

traverseData(data, "data");
var end = data;

正如您所看到的,我正在构建一个简单的递归来创建一个用于图表的新对象。如果您注意到数据对象,第四个条目的 y 值设置为 null。如果我用数字填写,一切都会按预期进行。但是,如果将其保留为空,我会收到错误。知道为什么吗?

编辑:有问题的行是在polyfill 中抛出错误的。我已在 block 打击中评论了它

        if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) { // Throws error here
result.push(dontEnums[i]);
}
}
}

最佳答案

首先,用于检测 null 值的逻辑存在问题:

if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}

如果objnull,则typeof obj将为“object”

试试这个:

if (typeof obj !== 'object' && typeof obj !== 'function' || obj === null)

现在,您将收到自定义Object.keys Called on non-object 错误。

这个null是由于

Object.prototype.toString.call(data[keys[i]]) == "[object Object]"

根据 ECMAScript 5,Object.prototype.toString.call(null) 必须返回 “[object Null]”。但在旧浏览器上,它可能会生成 "[object Object]"

相反,您可以使用

if(typeof data[keys[i]]=== 'object' && data[keys[i]])

关于javascript - IE8 对象当对象包含空值时出现预期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28773851/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com