Javascript bind如何自动将事件参数传递给事件处理程序?

Javascript bind如何自动将事件参数传递给事件处理程序?,javascript,reactjs,Javascript,Reactjs,报告指出: this.deleteRow(id,e)}>删除行 删除行 在这两种情况下,表示React事件的e参数都是 作为ID后的第二个参数传递。使用arrow函数,我们 必须显式地传递它,但使用bind时,任何进一步的参数都是 自动转发 因此,当使用bind时,事件将作为参数传递给回调中的事件处理程序——这是如何/为什么工作的 如果你检查bind的polyfill,你就会得到答案,我指的是bind到底做了什么 Bind基本上返回一个新函数,无论何时调用该函数(由Bind返回),它都会合并所

报告指出:

this.deleteRow(id,e)}>删除行
删除行
在这两种情况下,表示React事件的e参数都是 作为ID后的第二个参数传递。使用arrow函数,我们 必须显式地传递它,但使用bind时,任何进一步的参数都是 自动转发


因此,当使用
bind
时,事件将作为参数传递给回调中的事件处理程序——这是如何/为什么工作的

如果你检查bind的
polyfill
,你就会得到答案,我指的是bind到底做了什么

Bind基本上返回一个新函数,无论何时调用该函数(由Bind返回),它都会合并所有参数(绑定期间传递的参数和传递给Bind返回的函数的参数),然后将其传递给原始函数

选中此项:


谢谢,这很有意思,但我还是不确定这个活动是如何通过的。也许你说它是在那个特定的上下文中调用的,但对我来说,这段代码并不清楚它是如何完成的这里,arg将是上下文,args将是所有合并的参数。在这行
args.push.apply(args,参数)事件将在
参数中可用。在每个函数中,
参数
变量(预定义)表示传递给该函数的所有参数。请查看有关相同内容的更多详细信息。
<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
if (!Function.prototype.bind) (function(){
  var ArrayPrototypeSlice = Array.prototype.slice;
  Function.prototype.bind = function() {
    var thatFunc = this, thatArg = arguments[0];

    // ↓↓↓↓↓↓↓↓↓↓↓ argument bound to function
    var args = ArrayPrototypeSlice.call(arguments, 1);


    if (typeof thatFunc !== 'function') {
      throw new TypeError('Function.prototype.bind - ' + 'what is trying to be bound is not callable');
    }
    return function(){

      // ↓↓↓↓↓↓↓↓↓↓ check here, this line is basically merging all the arguments
      args.push.apply(args, arguments);


      // ↓↓↓↓↓↓↓↓↓↓ here, it will call the function with particular context and all the parameters
      return thatFunc.apply(thatArg, args);
    };
  };
})();