Javascript : event is not calling submitted bind(. .)-回调副本:-/

标签 javascript callback bind

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>

在此处调试代码:http://www.robosoft.de/forums/bug.htm

最佳答案

最好的解决方案是:

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/

相关文章:

javascript - ReactJS(初学者)使用 Youtube API - 返回的对象不允许我访问 VideoId 属性

scala - 澄清永远不会完成的 Scala Future 及其对其他回调的影响

javascript - 如何将值从一个变量绑定(bind)到其他变量

javascript绑定(bind)回调到任何函数

node.js - Supertest 可以帮助我验证我的 API 是否返回回调/jsonp 函数吗?

Javascript绑定(bind)函数实现

javascript - 在 Select2 中显示多列

javascript - jQuery + Flash 媒体播放或 YouTube : call function after flv finishes playing?

Javascript 数组在填充时显示 0 长度

c++ - postgresql查询c++回调