js中在tx.executeSql内赋值的数组跳出函数后不能按下标查找数组,为什么?
代码如下:
//JavaScript Document
$(function ($) {
var array=[];
var db = openDatabase('mydb', '1.0', 'Test DB', 2*1024*1024);
$(function ($){
init();
show();
});
function init(){
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS USER (id, username, sex, age, phone, address )');
tx.executeSql('INSERT INTO USER (id, username, sex, age, phone, address ) VALUES (?, ?, ?, ?, ?, ?)',[1, '李明', '男', 12, 13850501234, '福建']);
tx.executeSql('INSERT INTO USER (id, username, sex, age, phone, address ) VALUES (?, ?, ?, ?, ?, ?)',[2, '周福', '男', 23, 13850502345, '上海']);
});
}
function show(){
db.transaction(function (tx){
tx.executeSql('select * from USER',[],function (tx,results){
var len = results.rows.length;
var i;
for(i=0;i<len;i++){
let object;
object = results.rows.item(i);
array[i]={id:object.id,username:object.username,sex:object.sex,age:object.age,phone:object.phone,address:object.address};
}
**console.log(array[1]);**
});
**console.log(array[1]);**
});
}
}
运行结果:
usermanager.js:30 undefined
usermanager.js:28 {id: 2, username: "周福", sex: "男", age: 23, phone: 13850502345, …}