gpt4 book ai didi

javascript - 使用绑定(bind)将函数的参数保存在 "this"范围内

转载 作者:行者123 更新时间:2023-11-30 18:06:20 26 4
gpt4 key购买 nike

我在玩 bind,以下是可行的:

webSQL.InsertTransaction = function(qry,CurrentRow) {
var local = {};
// Clone the webSQL.Insert function and add 2 parameters:
local.InsertTransaction = webSQL.Insert.bind(this,qry,CurrentRow);
// Call webSQL.Insert(qry,CurrentRow,Transaction)
dbo.transaction(local.InsertTransaction);
}
webSQL.Insert = function(qry,CurrentRow,Transaction) {}

我想进一步简化它。我能不能以某种方式不必指定参数范围内的 2 个变量,而是执行类似的操作:

local.InsertTransaction = webSQL.Insert.bind(webSQL.InsertTransaction)

也许吧。我的想法是,然后 webSQL.Insert 可以从它的“this.arguments”中引用 qry 和 CurrentRow。

最佳答案

我不确定您为什么首先使用分配给 local 变量的对象。

您所做的只是给它一个函数,然后立即取回该函数。为什么不跳过这一步?

webSQL.InsertTransaction = function(qry,CurrentRow) {
dbo.transaction(webSQL.Insert.bind(this,qry,CurrentRow));
}

关于javascript - 使用绑定(bind)将函数的参数保存在 "this"范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15725049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com