Web SQL 游戏 - 将额外参数传递给匿名函数
我正在尝试制作一个 javascript 游戏,并使用 WebSQL 来存储游戏数据。我创建了自己的数据库选择函数,用于格式化查询结果然后返回它们。
在这个特定的实例中,我将返回的对象传递给结果函数。我还想传递其他两个变量,但我不断收到此错误“Uncaught ReferenceError:planetInfo 未定义”。如果有人可以提供帮助,我将不胜感激。提前谢谢您。
我也尝试过使用“selectRowPlanets('query',outcomes(shipInfo,arrivalNumber));”,但仍然没有运气。
/** db.js **/
function selectRowPlanets(query, callBack){ // <-- extra param
var result = [];
db.transaction(function (tx) {
tx.executeSql(query, [], function(tx, rs){
for(var i=0; i<rs.rows.length; i++) {
var row = rs.rows.item(i);
result[i] = { id: row['id'],
name: row['name'],
owner: row['owner'],
colum: row['colum'],
row: row['row'],
ships: row['ships'],
production: row['production'],
percent: row['percent']
}
}
callBack(result); // <-- new bit here
}, errorHandler);
});
}
/** function.js **/
function selectDestination(shipInfo, arrivalNumber) {
selectRowPlanets('SELECT * FROM planets', outcomes(planetInfo, shipInfo, arrivalNumber));
}
function outcomes(planetInfo, shipInfo, arrivalNumber){
console.log(arguments);
}
I'm trying to make a javascript game and I'm using WebSQL to store game data. I have created my own database selection function that formats the query results and then returns them.
In this specific instance I pass the returned object to the outcomes function. I would like to also pass in two other variables but I keep getting this error "Uncaught ReferenceError: planetInfo is not defined". If anyone could help It'd be very appreciated. Thank you in advanced.
I have also tried using "selectRowPlanets('query',outcomes(shipInfo, arrivalNumber));", but still no luck.
/** db.js **/
function selectRowPlanets(query, callBack){ // <-- extra param
var result = [];
db.transaction(function (tx) {
tx.executeSql(query, [], function(tx, rs){
for(var i=0; i<rs.rows.length; i++) {
var row = rs.rows.item(i);
result[i] = { id: row['id'],
name: row['name'],
owner: row['owner'],
colum: row['colum'],
row: row['row'],
ships: row['ships'],
production: row['production'],
percent: row['percent']
}
}
callBack(result); // <-- new bit here
}, errorHandler);
});
}
/** function.js **/
function selectDestination(shipInfo, arrivalNumber) {
selectRowPlanets('SELECT * FROM planets', outcomes(planetInfo, shipInfo, arrivalNumber));
}
function outcomes(planetInfo, shipInfo, arrivalNumber){
console.log(arguments);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您运行此代码时:
outcomes(shipInfo,arrivalNumber)
的输出作为回调传递,而不是实际的函数。创建一个调用您的代码的匿名函数包装器:
When you run this code:
The output of
outcomes(shipInfo, arrivalNumber)
is passed as the callback, not the actual function.Make an anonymous function wrapper that calls your code: