Anonymous javascript functions

Can someone 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 looked here for information, but I'm still in the dark.

In my opinion, I understand that the saveAsNew method of an object is defined as an anonymous function, which first creates a timestamp and creates a local reference to itself (note = this), and then calls the db object's transaction method, providing this method with a parameter, which is another parameter anonymous function with argument tx . But I do not understand where tx comes 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

+4
source share
7 answers

You could use any variable if the same variable is used in the definition of your anonymous method. The transaction method will pass the value as the first parameter when you call your anonymous method, and it will be assigned the value tx.

0
source

First, tx is a parameter definition. You can choose any name, it can also be a bar . It is no different from defining a function as

 function foo(bar) { } 

If you're wondering who passes this argument, then this is probably db.transaction . You pass the function as a callback [Wikipedia] . Somewhere db.transaction will call:

 callback(transaction); 

Perhaps this example helps to understand:

 function hello(callback) { // do some very important world changing computations... then: callback('Hello '); } hello(function(foobar) { alert(foobar + 'Tim!'); }); // alerts 'Hello Tim!' 

Here, the hello function passes one parameter to the callback function.

+1
source

If I knew that transaction() , I could help you more.

But I believe that this is what the function returns.

Like a click, event returns

So, somewhere in the transaction code, the variable is sent to a function, which you can then name and use in your code.


From an early code:

 db = openDatabase("NoteTest", "1.0", "HTML5 Database API example", 200000); 

You should learn that openDatabase has a transaction function.

Here you can read openDatabase()

0
source

The saveAsNew function returns a function that takes tx as an argument:

 var new = saveAsNew(); new(tx); 

as to where tx comes from, it’s impossible to say from this fragment.

0
source

The db.transaction () function must take an argument, which is a function that takes one parameter (the function (tx) in your example).

It is probably documented as db.transaction (callback).

The tx parameter is sent to the callback function (anonymous function (tx) in your case) by the db.transaction () function.

0
source

Your code is equivalent to this:

  saveAsNew: function() { this.timestamp = new Date().getTime(); var note = this; function booga(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]); } db.transaction(booga); }, 

Where does the “tx” in “booga (tx)” come from? Answer: The one who calls booga will pass the parameter, and tx is the name we gave to this parameter.

0
source

To call the db.transaction() function, one parameter is required. This parameter itself is a function called by db.transaction() , and when it calls this function, it passes one parameter to it. The name tx can be anything, it's just the name for the first parameter of this function.

Your code could be written using this function, which you pass to db.transaction() as an anonymous function, for example, you are here:

 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]); }); 

or it could be written in a way where it is a little more obvious what is happening, but not so compact:

 function writeSql(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]); } db.transaction(writeSql); 
0
source

Source: https://habr.com/ru/post/1368961/


All Articles