简体   繁体   English

javascript回调函数参数

[英]javascript callback function parameters

I want to call a callback function using javascript, as you can see from this example code from http://docs.phonegap.com/en/3.0.0/cordova_storage_storage.md.html#database_size 我想使用javascript调用回调函数,如您从http://docs.phonegap.com/en/3.0.0/cordova_storage_storage.md.html#database_size的此示例代码所见

function queryDB(tx) {
    tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}

function querySuccess(tx, results) {
    console.log("Returned rows = " + results.rows.length);
    // this will be true since it was a select statement and so rowsAffected was 0
    if (!results.rowsAffected) {
        console.log('No rows affected!');
        return false;
    }
    // for an insert statement, this property will return the ID of the last inserted row
    console.log("Last inserted row ID = " + results.insertId);
}

function errorCB(err) {
    alert("Error processing SQL: "+err.code);
}

var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(queryDB, errorCB);

The method "queryDB" is called without any parameters, but the function is defined to receive the variable "tx". 调用方法“ queryDB”时不带任何参数,但是已定义该函数以接收变量“ tx”。 Coming from PHP, I can't understand how this works, because it does. 来自PHP,我不明白它是如何工作的,因为它可以。 Also there is querySuccess which receives 2 parameters and also works. 还有querySuccess,它接收2个参数并且也可以工作。

My problem is that I need to send 1 my own parameter, so I have modified the call for queryDB like this: 我的问题是我需要发送1我自己的参数,所以我修改了对queryDB的调用,如下所示:

var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
db.transaction(queryDB(myParam), errorCB);

and

function queryDB(tx, myParam) {
    tx.executeSql('SELECT * FROM' + myParam, [], querySuccess, errorCB);
}

But is does not work. 但是是行不通的。

1) How come there are unsent params being received? 1)为什么会收到未发送的参数? 2) How can I add my own params without breaking it? 2)如何添加自己的参数而不破坏它?

Try this: 尝试这个:

db.transaction(function() { queryDB(tx, myParam); }, errorCB);

function queryDB(tx, myParam) {
    //logic here
}

The transaction function is looking for callback methods which do not include parameters. 事务功能正在寻找不包含参数的回调方法。 You can 'hijack' this by assigning an anonymous function that calls the function you want with parameters. 您可以通过分配匿名函数来“劫持”,该匿名函数使用参数调用所需的函数。

When you write 当你写

db.transaction (queryDB, errorCB);

you are passing as a parameter the function itself, but if you try this: 您将函数本身作为参数传递,但是如果尝试这样做:

db.transaction (queryDB(myParam), errorCB);

you'd be passing as a parameter the result of the function, which will generate an error. 您将函数的结果作为参数传递,这将产生错误。

db.transacción receives parameter are 2 functions, If you need to pass custom parameter simply write them in the declaration db.transacción接收参数是2个函数,如果您需要传递自定义参数,只需将它们写在声明中

function queryDB(tx, myParam) {
    //Using your myParam
    tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
}
...
db.transaction(queryDB, errorCB);

Remember that javascript is an interpreted programming language and is not object-oriented 请记住,javascript是一种解释型编程语言,不是面向对象的

Ok, so I think I have found the answer. 好的,我想我已经找到了答案。 Reading from here http://theelitist.net/passing-additional-arguments-to-a-callback-function-in-javascript I have realised that Dawson Loudon's response was almost complete, but he missed the "tx" variable that should be passed on the first function that calls the 2nd one, that would be: 从这里阅读http://theelitist.net/passing-additional-arguments-to-a-callback-function-in-javascript我已经意识到Dawson Loudon的响应几乎是完整的,但是他错过了应该是“ tx”变量传递给第一个调用第二个函数的函数,它是:

db.transaction(function(tx) { queryDB(tx, myParam); }, errorCB);

function queryDB(tx, myParam) {
    //logic here
}

Thanks for your time and help. 感谢您的时间和帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM