Web SQL 游戏 - 将额外参数传递给匿名函数

发布于 2024-12-19 05:03:20 字数 1344 浏览 1 评论 0原文

我正在尝试制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何时共饮酒 2024-12-26 05:03:20

当您运行此代码时:

selectRowPlanets('query', outcomes(shipInfo, arrivalNumber));

outcomes(shipInfo,arrivalNumber)输出作为回调传递,而不是实际的函数。

创建一个调用您的代码的匿名函数包装器:

selectRowPlanets('query', function() { outcomes(shipInfo, arrivalNumber) });

When you run this code:

selectRowPlanets('query', outcomes(shipInfo, arrivalNumber));

The output of outcomes(shipInfo, arrivalNumber) is passed as the callback, not the actual function.

Make an anonymous function wrapper that calls your code:

selectRowPlanets('query', function() { outcomes(shipInfo, arrivalNumber) });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文