bind() 应该返回原始函数的副本。但是,当将此副本作为回调传递时,会调用原始函数:-( 使用 Chrome、Firefox 和 Edge 进行测试。那么我的推理错误在哪里?
<html><body>
<script>
// new api to wrap these nested Cordova callbacks ..
ScanDir = function (sPath, rCallback, iStep){
yes = _Yes.bind(rCallback); // should return a copy/newInstance of _Yes !?!
yes.iStep = iStep;
// simulate a cordova callback ..
document.getElementById("test").addEventListener("click", yes);
}
_Yes = function YES(o){
// this is not the new instance returned by bind(..):
alert(_Yes.iStep); // = undefined
alert(YES.iStep); // = undefined
alert(arguments.callee.iStep); // = undefined
alert(yes.iStep); // wrong, 'yes' must not be global...
}
</script>
<span id="test">click here to trigger callback</span>
<script>
// code that uses my new api..
function Go(v){
if (!v.iStep) return ScanDir(v,Go,1);
alert("continue with switch("+v.iStep+")");
}
Go("Music/");
</script>
</body></html>
最佳答案
最好的解决方案是:
yes = _Yes.bind({callback:rCallback,iStep:iStep});
在里面_是的,您可以访问:
this.callback();
alert(this.iStep);
不,bind 不会创建新函数,它会绑定(bind)该函数...
关于Javascript : event is not calling submitted bind(. .)-回调副本:-/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43829875/