好的,所以在网页上,我有一个 JavaScript 对象,我将其用作关联数组。这在页面加载时静态存在于脚本块中:
var salesWeeks = {
"200911" : ["11 / 2009", "Fiscal 2009"],
"200910" : ["10 / 2009", "Fiscal 2009"],
"200909" : ["09 / 2009", "Fiscal 2009"],
"200908" : ["08 / 2009", "Fiscal 2009"],
"200907" : ["07 / 2009", "Fiscal 2009"],
"200906" : ["06 / 2009", "Fiscal 2009"],
"200905" : ["05 / 2009", "Fiscal 2009"],
"200904" : ["04 / 2009", "Fiscal 2009"],
"200903" : ["03 / 2009", "Fiscal 2009"],
"200902" : ["02 / 2009", "Fiscal 2009"],
"200901" : ["01 / 2009", "Fiscal 2009"],
"200852" : ["52 / 2008", "Fiscal 2009"],
"200851" : ["51 / 2008", "Fiscal 2009"]
};
键/值对的顺序是有意的,因为我将对象转换为 HTML 选择框,如下所示:
<select id="ddl_sw" name="ddl_sw">
<option value="">== SELECT WEEK ==</option>
<option value="200911">11 / 2009 (Fiscal 2009)</option>
<option value="200910">10 / 2009 (Fiscal 2009)</option>
<option value="200909">09 / 2009 (Fiscal 2009)</option>
<option value="200908">08 / 2009 (Fiscal 2009)</option>
<option value="200907">07 / 2009 (Fiscal 2009)</option>
<option value="200906">06 / 2009 (Fiscal 2009)</option>
<option value="200905">05 / 2009 (Fiscal 2009)</option>
<option value="200904">04 / 2009 (Fiscal 2009)</option>
<option value="200903">03 / 2009 (Fiscal 2009)</option>
<option value="200902">02 / 2009 (Fiscal 2009)</option>
<option value="200901">01 / 2009 (Fiscal 2009)</option>
<option value="200852">52 / 2008 (Fiscal 2009)</option>
<option value="200851">51 / 2008 (Fiscal 2009)</option>
</select>
...使用看起来像这样的代码(从函数中截取):
var arr = [];
arr.push(
"<select id=\"ddl_sw\" name=\"ddl_sw\">" +
"<option value=\"\">== SELECT WEEK ==</option>"
);
for(var key in salesWeeks)
{
arr.push(
"<option value=\"" + key + "\">" +
salesWeeks[key][0] + " (" + salesWeeks[key][1] + ")" +
"<\/option>"
);
}
arr.push("<\/select>");
return arr.join("");
这在 IE、FireFox 和 Opera 中都可以正常工作。
然而在 Chrome 中,这个顺序很奇怪:
<select id="ddl_sw" name="ddl_sw">
<option value="">== SELECT WEEK ==</option>
<option value="200852">52 / 2008 (Fiscal 2009)</option>
<option value="200908">08 / 2009 (Fiscal 2009)</option>
<option value="200906">06 / 2009 (Fiscal 2009)</option>
<option value="200902">02 / 2009 (Fiscal 2009)</option>
<option value="200907">07 / 2009 (Fiscal 2009)</option>
<option value="200904">04 / 2009 (Fiscal 2009)</option>
<option value="200909">09 / 2009 (Fiscal 2009)</option>
<option value="200903">03 / 2009 (Fiscal 2009)</option>
<option value="200905">05 / 2009 (Fiscal 2009)</option>
<option value="200901">01 / 2009 (Fiscal 2009)</option>
<option value="200910">10 / 2009 (Fiscal 2009)</option>
<option value="200911">11 / 2009 (Fiscal 2009)</option>
<option value="200851">51 / 2008 (Fiscal 2009)</option>
</select>
注意:这个顺序虽然很奇怪,但在随后的刷新中不会改变。它总是按这个顺序。
那么,Chrome 在做什么呢?它如何处理循环的一些优化?
首先,依赖键/值对在任何关联数组中声明的顺序是错误的吗?
我以前从未质疑过它,我只是假设这个顺序会成立,因为这种技术在其他浏览器中一直对我有用。但我想我从未见过它在任何地方都说订单是有保证的。也许不是?
任何见解都会很棒。谢谢。