作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
YDKJS 书中包含一个描述 soft binding 的部分。效用:
if (!Function.prototype.softBind) {
Function.prototype.softBind = function(obj) {
var fn = this,
curried = [].slice.call( arguments, 1 ),
bound = function bound() {
return fn.apply(
(!this ||
(typeof window !== "undefined" &&
this === window) ||
(typeof global !== "undefined" &&
this === global)
) ? obj : this,
curried.concat.apply( curried, arguments )
);
};
bound.prototype = Object.create( fn.prototype );
return bound;
};
}
我无法理解这一行:curried.concat.apply( curried,arguments )
。为什么我们要将已经柯里化(Currying)的参数与参数对象而不是简单地使用柯里化(Currying)数组:
...
) ? obj : this,
curried
);
};
bound.prototype = Object.create( fn.prototype );
...
最佳答案
当您调用绑定(bind)函数时,首先会传递绑定(bind)参数,然后传递调用绑定(bind)版本的新参数。
即
function log(a, b, c, d, e) {
console.log([a, b, c, d, e]);
}
log(1, 2, 3, 4, 5);
const bound = log.bind(null, 1, 2, 3);
bound(4, 5);
如果您没有连接绑定(bind)参数和新参数,那么您只能记录 1、2 和 3。
关于javascript - 你不知道的JS书,软化 'this'绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53251976/
YDKJS 书中包含一个描述 soft binding 的部分。效用: if (!Function.prototype.softBind) { Function.prototype.softB
我是一名优秀的程序员,十分优秀!