javascript 参数匿名函数

发布于 2024-12-01 12:34:11 字数 1136 浏览 1 评论 0原文

有人可以解释一下下面代码片段中的 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]);
                 }); 
    },
    .
    .
    .

FULL CODE PASTE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小兔几 2024-12-08 12:34:12

函数调用 db.transaction() 本身需要一个参数。该参数本身就是 db.transaction() 将调用的一个函数,当它调用该函数时,它将向其传递一个参数。名称 tx 可以是任何内容,它只是该函数的第一个参数的名称。

您的代码可以使用您传递给 db.transaction() 的函数作为匿名函数编写,就像您在此处所做的那样:

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

或者可以这样编写,这样更明显正在发生,但并不那么紧凑:

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

The function call db.transaction() itself requires one parameter. That parameter is itself a function that db.transaction() will call and when it calls that function, it will pass it one parameter. The name tx 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:

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 have been written this way where it's a little more obvious what is happening, but not as 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);
狼性发作 2024-12-08 12:34:11

首先,tx是一个参数定义。您可以选择任何名称,也可以是 bar。它与定义函数没有什么不同,

function foo(bar) {

}

如果您想知道“谁”正在传递此参数,那么它可能是db.transaction。您正在将函数作为回调传递[维基百科]。 db.transaction 会在某个地方调用:

callback(transaction);

也许这个例子有助于理解:

function hello(callback) {
    // do some very important world changing computations... then:
    callback('Hello ');
}

hello(function(foobar) {
    alert(foobar + 'Tim!');
});
// alerts 'Hello Tim!'

这里,hello 函数将一个参数传递给回调函数。

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

function foo(bar) {

}

If you wonder "who" is passing this argument, then it is probably db.transaction. You are passing the function as a callback [Wikipedia]. Somewhere db.transaction will call:

callback(transaction);

Maybe this example helps it 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.

带刺的爱情 2024-12-08 12:34:11

您可以使用任何变量,只要在匿名方法定义中使用相同的变量即可。当调用匿名方法时,交易方法将传递一个值作为第一个参数,并将其分配给 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.

空城旧梦 2024-12-08 12:34:11

函数 saveAsNew 返回一个以 tx 作为参数的函数:

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

至于 tx 来自哪里,从这个片段中无法说出。

The function saveAsNew 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 snippet.

狠疯拽 2024-12-08 12:34:11

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.

仅此而已 2024-12-08 12:34:11

您的代码相当于:

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

“booga(tx)”中的“tx”来自哪里?答:调用 booga 的人都会传递一个参数,tx 是我们给该参数起的名字。

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" come from in "booga(tx)"? Answer: Whoever calls booga will pass a parameter, and tx is the name we gave to that parameter.

不乱于心 2024-12-08 12:34:11

如果我知道 transaction() 是什么,我也许可以为您提供更多帮助。

但我相信这是函数返回的东西。

就像点击有一个返回的事件一样

,在事务代码中的某个地方,一个变量被发送到函数,然后您可以在代码中命名和使用该函数。


从前面的代码来看:

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

您必须研究具有 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 returned

So 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:

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

You have to look into what openDatabase does that has the transaction function.

You can read up on openDatabase() here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文