gpt4 book ai didi

javascript - 使用 Dojo lang.hitch 将参数传递给被调用函数

转载 作者:行者123 更新时间:2023-11-30 13:50:45 27 4
gpt4 key购买 nike

我在 ESRI 的 Web AppBuilder 环境(使用 ESRI Javascript 3.x API)中使用 dojo。

无论如何,我创建了一个按钮,并且在该按钮的 onClick 方法中,我希望能够使用 lang.hitch 调用另一个函数(以将函数保留在范围内)。但是被调用的函数是带参数的,我好像传不进去,只能调用函数,像这样。

this.myDialogBtn1 = new Button({
标签:“创建新位置”,
禁用:假,
onClick: lang.hitch(this, this._createNewLocation)
}).placeAt(this.createNewLoc)

当然,我的 _createNewLocation 函数需要接受一个参数,就像这样。

_createNewLocation(param){...做一些事情}

我不确定如何将该参数传递给 onClick 方法。仅仅添加这样的参数是行不通的。它抛出类型错误。有什么想法吗?

lang.hitch(this, this._createNewLocation(param))

最佳答案

只绑定(bind)参数

onClick: lang.hitch(this, this._createNewLocation.bind(this,param));

这会将参数作为第一个参数传递给函数,this 也是您绑定(bind)函数的上下文

正如在下面的评论中指出的那样,hitch 是 bind 的 dojos 实现,然后也应该采用参数,但如果是这种情况,您甚至不需要使用 hitch,只需调用

点击时:this._createNewLocation.bind(this,param);

关于javascript - 使用 Dojo lang.hitch 将参数传递给被调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58295030/

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