Javascript 为什么Function.prototype.bind返回构造函数?

Javascript 为什么Function.prototype.bind返回构造函数?,javascript,Javascript,我在看Function.prototype.bind方法的polyfill if (!Function.prototype.bind) { Function.prototype.bind = function(oThis) { if (typeof this !== 'function') { // closest thing possible to the ECMAScript 5 // internal IsCallable function

我在看
Function.prototype.bind
方法的polyfill

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;
  };
}
有人能给我解释一下我们为什么要这样做吗?如果我没有使用
new
操作符调用我的
fn
,我认为这是不必要的。如果不是通过新操作符调用,为什么返回的
fn
需要是
构造函数

fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();    
return fBound;
我们可以做下面这样的事情,而不是创建一个新的构造函数吗

fBound.prototype = Object.create(this.prototype);
return fBound

你可以。。。如果支持
Object.create

但如果您是polyfill
函数.prototype.bind
,则很可能您也必须polyfill
对象.create

Object.create
的多边形填充基本上就是您认为不必要的部分:

Object.create=(函数(){
var fNOP=函数(){};
返回函数(原型){
如果(arguments.length>1)抛出错误(“不支持第二个参数”);
if(typeof prototype!=“object”)抛出TypeError(“参数必须是对象”);
fNOP.prototype=原型;
var结果=新fNOP();
fNOP.prototype=null;
返回结果;
};
})();

[此注释不属于主题]如果
Function.prototype.bind
需要多填充,则
Object.create
也需要多填充,因为两者都来自ES5。因此,您不能使用
对象对
函数.prototype.bind
进行多边形填充。创建
。此外,
Object.create
实际上更难进行多填充。实际上,如果考虑到边缘情况,例如
Object.create(null)
@Leo True,则
Object.create
似乎不可能进行多填充,但在某些情况下可以进行多填充。对于那些人,警察会做OP认为不必要的事。是的,我提到了边缘案件。