深入一点-使用bind的时候发生了什么呢
从规范来看,Function.prototype.bind 是如何工作,以及如何来模拟bind操作。 简单示例如下简单示例,普通对象 testObj 内部有一个b函数,接受一个普通参数,若参数为空则输出 this.a。 const testObj = { a: 3, b: function(args) { console.log(args || this.a); },};testObj.b()testObj.b(23)const c = testObj.bc()c(23)const c1 = testObj.b.bind(testObj, 50)c1(70)查看结果: testObj.b 被重新赋值给 c 后,函数的的执行上下文已经改变,导致输出为 undefined。通过上面例子,如果采用 bind 后,则可以改变 testObj的执行上下文,并可以把默认值传递到参数函数列表. 倘若在 testObj.b内,加入 console.log(arguments), 则可以看到如下输出: 50 70bind 函数bind函数是,Function 原型链上的函数,主要是改变函数执行上下文的同时,可以传入函数参数值,并返回新的函数 如图是mdn上的定义, bind产生的函数就一个偏函数,就是说使用bind可以参数一个函数,然后接受新的参数。 In computer science, partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity.详细可看: https://github.com/mqyqingfeng/Blog/issues/43规范定义在EcmaScript的规范 15.3.4.5 中如下截图: ...