我正在ESRI的Web AppBuilder环境(使用ESRI Javascript 3.x API)中使用dojo。
无论如何,我创建了一个按钮,并且希望在该按钮的onClick方法中使用lang.hitch调用另一个函数(以使函数保持作用域内)。但是被调用的函数需要一个参数,我似乎无法传递它。我只能这样调用该函数。
this.myDialogBtn1 = new Button({
label: "Create New Location",
disabled: false,
onClick: lang.hitch(this, this._createNewLocation)
}).placeAt(this.createNewLoc)
当然,我的_createNewLocation函数需要采用这样的参数。
_createNewLocation(param){...do stuff}
我不确定如何将该参数传递给onClick方法。仅添加这样的参数是行不通的。它抛出一个TypeError。有什么想法吗?
lang.hitch(this, this._createNewLocation(param))
仅绑定参数
onClick: lang.hitch(this, this._createNewLocation.bind(this,param));
这将把参数作为第一个参数传递给函数,this
也是绑定函数的上下文