回调函数的其他参数
问题描述:
我使用的是HTML5地理位置函数中的getCurrentPosition
,但需要将多个position
参数传递给成功回调函数。 (getCurrentPosition
回调仅默认提供position
参数)回调函数的其他参数
我该怎么做?
navigator.geolocation.getCurrentPosition(doOnSuccess, true);
function doOnSuccess(position, switchFlag) {
// process the position based on the value of switchFlag
}
我实际的代码是很多比这更复杂,doOnSuccess
功能重复使用多次,所以在其他地方使用匿名函数是不是一个真正的选择。
答
根据我上面的评论,答案的线索实际上是在这个问题 - 你使用一个匿名函数,然后用它来调用具有所有必需参数的正确函数。
navigator.geolocation.getCurrentPosition(function() {
doOnSuccess(position,true);
});
function doOnSuccess(position, switchFlag) {
// process the position based on the value of switchFlag
}
如果从'功能doOnSuccess..'离开了'switchFlag',你已经获得了'VAR switchFlag'的是......这难道不是为你工作,要么? – deceze
它实际上是我的示例代码,在这里引起混淆.. mySwitchFlag变量实际上并不存在于我的代码中,尽管根据您的评论创建它将是一个解决方案。实际上,我已经确定了我想要达到的目标 - 使用匿名函数来调用带有所需附加参数的doOnSuccess函数,例如, 'navigator.geolocation.getCurrentPosition(function(){doOnSuccess(position,true);});' –
我现在纠正了OP中的代码,使其更加清晰,我试图实现。 –