At a minimum, change this:
(至少,改变这个:)
function BlockID() {
var IDs = new Array();
images['s'] = "Images/Block_01.png";
images['g'] = "Images/Block_02.png";
images['C'] = "Images/Block_03.png";
images['d'] = "Images/Block_04.png";
return IDs;
}
To this:
(对此:)
function BlockID() {
var IDs = new Object();
IDs['s'] = "Images/Block_01.png";
IDs['g'] = "Images/Block_02.png";
IDs['C'] = "Images/Block_03.png";
IDs['d'] = "Images/Block_04.png";
return IDs;
}
There are a couple fixes to point out.
(有几点需要指出。)
First , images
is not defined in your original function, so assigning property values to it will throw an error.(首先 , images
未在原始函数中定义,因此为其指定属性值将引发错误。)
We correct that by changing images
to IDs
.(我们通过将images
更改为IDs
来纠正此问题。)
Second , you want to return an Object
, not an Array
.(其次 ,您想要返回一个Object
,而不是一个Array
。)
An object can be assigned property values akin to an associative array or hash -- an array cannot.(可以为对象分配类似于关联数组或散列的属性值 - 数组不能。)
So we change the declaration of var IDs = new Array();
(所以我们改变var IDs = new Array();
的声明var IDs = new Array();
)
to var IDs = new Object();
(to var IDs = new Object();
)
.(。)
After those changes your code will run fine, but it can be simplified further .
(在这些更改之后,您的代码将运行正常,但可以进一步简化 。)
You can use shorthand notation (ie, object literal property value shorthand) to create the object and return it immediately:(您可以使用简写表示法(即,对象文字属性值简写)来创建对象并立即返回它:)
function BlockID() {
return {
"s":"Images/Block_01.png"
,"g":"Images/Block_02.png"
,"C":"Images/Block_03.png"
,"d":"Images/Block_04.png"
};
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…