我尝试在Javascript中创建受欺骗的索引。这是我的代码。在Javascript中创建自定义数值数组索引
var map = [];
function createIndexIfNotExists (posx,posy){
if(typeof(map[posx]===undefined)){
map[posx] = {};
console.log("created: map["+posx+"] typeof="+typeof(map[posx])); //typeof object
}
if(typeof(map[posx][posy]===undefined)){
map[posx][posy] = [];
console.log("created: map["+posx+"]["+posy+"]
typeof="+typeof(map[posx])); //typeof object
}
map[posx][posy].push({'posx':posx, 'posy':posy }); }
createIndexIfNotExists(10,5);
createIndexIfNotExists(10,6);
但结果是这样的。
created: map[10] typeof=object
created: map[10][5] typeof=object
created: map[10] typeof=object
created: map[10][6] typeof=object
为什么要创建map[10]
两次,如果是typeof运算和object
不undefined
?