javascript 参数匿名函数
有人可以解释一下下面代码片段中的 function(tx)
吗,来自此页面:http://www.webkit.org/demos/sticky-notes/。 tx
在哪里以及如何分配?我已经在此处查找信息,但仍然一无所知。
我认为我理解的是,对象的 saveAsNew 方法被定义为一个匿名函数,它首先创建一个时间戳并创建对其自身的本地引用(note=this),然后调用db 对象的 transaction 方法,为该方法提供一个参数,该参数是另一个具有参数 tx 的匿名函数。但我不明白 tx 来自哪里。
.
.
.
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]);
});
},
.
.
.
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]);
});
},
.
.
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
函数调用
db.transaction()
本身需要一个参数。该参数本身就是 db.transaction() 将调用的一个函数,当它调用该函数时,它将向其传递一个参数。名称 tx 可以是任何内容,它只是该函数的第一个参数的名称。您的代码可以使用您传递给 db.transaction() 的函数作为匿名函数编写,就像您在此处所做的那样:
或者可以这样编写,这样更明显正在发生,但并不那么紧凑:
The function call
db.transaction()
itself requires one parameter. That parameter is itself a function thatdb.transaction()
will call and when it calls that function, it will pass it one parameter. The nametx
can be anything, it's just a name for the first parameter to this function.Your code could have been written with that function that you are passing to
db.transaction()
as an anonymous function like you did here:or it could have been written this way where it's a little more obvious what is happening, but not as compact:
首先,
tx
是一个参数定义。您可以选择任何名称,也可以是bar
。它与定义函数没有什么不同,如果您想知道“谁”正在传递此参数,那么它可能是
db.transaction
。您正在将函数作为回调传递[维基百科]。 db.transaction 会在某个地方调用:也许这个例子有助于理解:
这里,
hello
函数将一个参数传递给回调函数。First,
tx
is a parameter definition. You can choose any name, it could also bebar
. It is not different from defining a function asIf you wonder "who" is passing this argument, then it is probably
db.transaction
. You are passing the function as a callback [Wikipedia]. Somewheredb.transaction
will call:Maybe this example helps it to understand:
Here, the
hello
function passes one parameter to the callback function.您可以使用任何变量,只要在匿名方法定义中使用相同的变量即可。当调用匿名方法时,交易方法将传递一个值作为第一个参数,并将其分配给 tx.
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.
函数 saveAsNew 返回一个以 tx 作为参数的函数:
至于 tx 来自哪里,从这个片段中无法说出。
The function saveAsNew returns a function that takes tx as an argument:
as to where tx comes from, it's impossible to say from this snippet.
db.transaction() 函数必须采用一个参数,该参数是一个采用一个参数的函数(在您的示例中为 function(tx) )。
它可能被记录为 db.transaction(callback)。
参数 tx 由 db.transaction() 函数发送到回调函数(在您的情况下为匿名函数(tx))。
The db.transaction() function must take an argument that is a function that takes one parameter (function(tx) in your example).
It is probably documented as db.transaction(callback).
The parameter tx is sent to the callback function (anonymous function(tx) in your case) by the db.transaction() function.
您的代码相当于:
“booga(tx)”中的“tx”来自哪里?答:调用 booga 的人都会传递一个参数,tx 是我们给该参数起的名字。
Your code is equivalent to this:
Where does the "tx" come from in "booga(tx)"? Answer: Whoever calls booga will pass a parameter, and tx is the name we gave to that parameter.
如果我知道
transaction()
是什么,我也许可以为您提供更多帮助。但我相信这是函数返回的东西。
就像点击有一个返回的
事件
一样,在
事务
代码中的某个地方,一个变量被发送到函数,然后您可以在代码中命名和使用该函数。从前面的代码来看:
您必须研究具有
transaction
函数的openDatabase
的用途。您可以在此处阅读
openDatabase()
If I knew what
transaction()
was I might be able to help you more.But I believe it is something that function returns.
Like click has a
event
that is returnedSo somewhere in the
transaction
code a variable is sent to the function which you can then name and use in your code.From earlier on the code:
You have to look into what
openDatabase
does that has thetransaction
function.You can read up on
openDatabase()
here