我有以下方法:
DBConnection.prototype.successHandler = function(){
console.log("DB_INFO: Schema created");
for (k in this) console.log(k);
this.setStatus(DB_STATUS_OK);
}
我在这样的事务中调用它:
DBConnection.prototype.createSchema = function(){
try {
this.c2db.transaction(
function(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS person(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL DEFAULT "");',
[], this.nullDataHandler, this.errorHandler);
tx.executeSql("INSERT INTO person(id, name) VALUES (NULL, 'miloud');",
[], this.nullDataHandler, this.errorHandler);
},
this.errorHandler,
this.successHandler
);
} catch(e){
console.log("DB_ERROR: error during insert with message: " + e);
return;
}
}
问题是我得到:Uncaught TypeError: Object [object Window] has no method 'setStatus' 这清楚地表明我正在访问的不是DBConnection
我在成功回调中使用的实例。怎么来的?this 在回调中指的是什么?有没有办法克服这个问题?
编辑
回调定义为:
DBConnection.prototype.errorHandler = function(errorMsg){
console.log("DB_ERROR: error creating schema with msg " + errorMsg.message);
}
DBConnection.prototype.successHandler = function(){
console.log("DB_INFO: Schema created");
for (k in this) console.log(k);
this.setStatus(DB_STATUS_OK);
}
而 setStatus 方法为
DBConnection.prototype.setStatus = function(str_status){
localStorage.setItem(db_name, str_status);
}
谢谢!