Android 5.1 未定义函数外的数组元素
Array element outside function is undefined with Android 5.1
我有一个带有一些 JavaScript 代码的 cordova 应用程序:
function test(){
a = new Array();
a['var_a']=5;
a['var_b']=10;
return a;
}
$(document).ready(function(){
return_arr = test();
console.log(return_arr['var_a']);
});
我的预期结果是 5
。在 Intel XDK Emulator 和带有 OS 10 的 Blackberry 中,我也得到 5
。但是,对于带有 Android 5.1 的 Cubot phone,我收到 undefined
。
什么会导致这种不同的行为?
我的解决方案是使 return_arr 全局化,但我仍然对为什么我的代码应该是错误的感兴趣?!
数组已编入索引,而您正在寻找属性。你最好使用对象,而不是数组...
function test(){
a = {}; // declare an empty object
a['var_a']=5;
a['var_b']=10;
return a;
}
$(document).ready(function(){
return_arr = test();
console.log(return_arr['var_a']);
});
我有一个带有一些 JavaScript 代码的 cordova 应用程序:
function test(){
a = new Array();
a['var_a']=5;
a['var_b']=10;
return a;
}
$(document).ready(function(){
return_arr = test();
console.log(return_arr['var_a']);
});
我的预期结果是 5
。在 Intel XDK Emulator 和带有 OS 10 的 Blackberry 中,我也得到 5
。但是,对于带有 Android 5.1 的 Cubot phone,我收到 undefined
。
什么会导致这种不同的行为? 我的解决方案是使 return_arr 全局化,但我仍然对为什么我的代码应该是错误的感兴趣?!
数组已编入索引,而您正在寻找属性。你最好使用对象,而不是数组...
function test(){
a = {}; // declare an empty object
a['var_a']=5;
a['var_b']=10;
return a;
}
$(document).ready(function(){
return_arr = test();
console.log(return_arr['var_a']);
});