【发布时间】:2014-12-10 21:38:29
【问题描述】:
我只是想真正理解以下来自 MDN 的代码。它是 Function.prototype.bind 的垫片:
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== 'function') {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
}
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP && oThis
? this
: oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
首先,为什么 aArgs 最初会忽略参数列表中的第一个参数?
其次,为什么 fToBind 采用 aArgs 数组并将其余参数连接起来?这不会创建一个 args 1 到 n 与 args 0 到 n 连接的数组吗?
我很困惑!
【问题讨论】:
标签: javascript this bind partial-application