开发者

Argument passed by reference

I have the following JavaScript function:

function insertIntoTable(title, text, sketchFileName, categoryId)
{
    var db = window.openDatabase('xxx', '1.0', 'xxx database', 5*1024*1024);
    db.transaction(
        function (tx) {
            if (sketchFileName == '')
            {
                tx.executeSql('INSERT INTO TableXXX (title, content, created, categoryID) VALUES (?, ?, ?, ?)', 
                            [title, text, Date.now(), categoryId], 
                            function (tr开发者_JAVA技巧ansaction, resultSet) {
                                if (resultSet.rowsAffected) {
                                    myNameSpace.returnedId = resultSet.insertId;
                                }
                            }, handleSQLError);
            }
        }, handleSQLError);
    );
}

Now I'm using a global variable, myNameSpace.returnedId, to get insertId.

Is there any way to add a new parameter to function insertIntoTable that references to myNameSpace.returnedId?


You can reference the variable with two arguments:

function insertIntoTable(title, text, sketchFileName, categoryId, nameSpace, variable)
{
    ...
    nameSpace[variable] = resultSet.insertId;
}

Call it like this:

insertIntoTable( ..., myNameSpace, 'returnedId' );

An alternative method is to use a setter function:

function insertIntoTable(title, text, sketchFileName, categoryId, setReturnedId)
{
    setReturnedId( resultSet.insertId );

}

Call it like this:

insertIntoTable( ..., function(value){ myNameSpace.returnedId = value } );


Change the function to this:

function insertIntoTable(title, text, sketchFileName, categoryId, nameSpace) {
...
...
nameSpace.returnedId = resultSet.insertId; // assign this here

and call the function via:

insertIntoTable(someTitle, someText, someSketchFileName, someCategoryId, myNamespace)

You'll be passing in a reference to myNamespace (will be called nameSpace within the function). You'll have access to all of myNameSpace's attributes then (and can assign things to them).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜