Search code examples
javascriptargumentsanonymous-functionopendatabase

javascript arguments anonymous function


Could someone please explain function(tx) in the code snippet below, from this page: http://www.webkit.org/demos/sticky-notes/. Where and how is tx assigned? I have looked here for information but am still in the dark.

What I think I understand is that that the saveAsNew method of the object is being defined as an anonymous function that first creates a timestamp and creates a local reference to itself (note=this), and then invokes the transaction method of the db object, providing to that method a parameter which is yet another anonymous function that has an argument tx. But I don't understand where tx is coming from.

   .
   .
   .
      saveAsNew: function()
         {
             this.timestamp = new Date().getTime();        
             var note = this;
             db.transaction(function (tx)   
                 {
                 tx.executeSql("INSERT INTO WebKitStickyNotes (id, note, timestamp, left, top, zindex) VALUES (?, ?, ?, ?, ?,                   ?)", [note.id, note.text, note.timestamp, note.left, note.top, note.zIndex]);
                 }); 
    },
    .
    .
    .

FULL CODE PASTE


Solution

  • You could have used any variable, so long as in the your anonymous method definition you use the same variable. The transaction method will pass a value as the first parameter when calling your anonymous method and it will be assigned to tx.