[英]Javascript : event is not calling submitted bind(..)-copy of callback :-/
bind() should return a copy of the original function.. Yet when passing this copy as a callback, the original function is called :-( Tested with Chrome, Firefox and Edge. So where is my error in reasoning ? 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>
debug the code here: http://www.robosoft.de/forums/bug.htm 在此处调试代码: http : //www.robosoft.de/forums/bug.htm
The best solution would be: 最好的解决方案是:
yes = _Yes.bind({callback:rCallback,iStep:iStep});
Inside _Yes you can access: 在_是内,您可以访问:
this.callback();
alert(this.iStep);
And no, bind does not create a new function, it binds the function... 不,bind不会创建新功能,它会绑定功能...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.