gpt4 book ai didi

javascript - ie 访问对象时出现 "...is null or not an object"错误?

转载 作者:行者123 更新时间:2023-12-02 20:28:57 25 4
gpt4 key购买 nike

我有一个 JavaScript 对象文字:

var things = {
"a": {
7: {
"b": "asdf",
"z": {
1: {
"name": "Ship0",
"loa": 100,
"draft": 5000
},
2: {
"name": "Ship1",
"loa": 100,
"draft": 5000
}
}
},
8: {
"b": "fdsa",
"z": {
5: {
"name": "Ship0",
"loa": 100,
"draft": 5000
},
6: {
"name": "Ship1",
"loa": 100,
"draft": 5000
}
}
}
}
};

...稍后在函数中,我这样做:

function p_get_index_of_existing_ship(customer_id, ship_id)
{
return someotherfunctionof(ship_id, things["a"][customer_id]["z"]);
}

它在 Firefox 中运行良好,但在 IE(7,8) 中我得到“错误:'things.a[...].z' 为 null 或不是对象。

这不是在 JavaScript 中访问此类关联数组(对象)的正确方法吗?

最佳答案

对象文字的键必须是有效的标识符名称。您的键是 78,它们不是有效名称(因为第一个(也是唯一的)字符是数字)。我建议改用字符串:

"7": { ... }

编辑:我查阅了规范...似乎数字文字是键的有效名称...但 IE 仍然可能不喜欢它们。尝试一下字符串你就会知道。

编辑:试试这个:

function p_get_index_of_existing_ship(customer_id, ship_id)
{
// return someotherfunctionof(ship_id, things["a"][customer_id]["z"]);
things["a"][customer_id]["z"];
}

IE 中是否仍然显示该错误?

关于javascript - ie 访问对象时出现 "...is null or not an object"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4475467/

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