bind函数polyfill源码

Function.prototype.bind = Function.prototype.bind || function(oThis) {
    var aArgs = Array.prototype.slice.call(arguments,1),
        //由于bind是原型方法,fToBind指调用bind的函数对象
        fToBind = this,
        F = function(){},
        fBound = function(){
            //fBound若作为构造函数,则this就是fBound构造出来的对象
            //构造函数中有return,若return的是标量,则忽略,return的是对象,则覆盖构造的实例
            return fToBind.apply(this instanceof F ? this : oThis || this,aArgs.concat(Array.prototype.slice.call(arguments)))
        };

    F.prototype = fToBind.prototype;
    fBound.prototype = new F();

    return fBound;
}

其中的 this instanceOf F是什么意思,能举个例子说明吗,多谢?

阅读 4.8k
3 个回答

从问题上看,instanceOf这个东东是一个运算符。它的意义是左操作数为右操作数的实例(即是否F为this的类),返回boolean值,就是这个意思。例子:

var time=new Date();
time instanceof Date;//true,time对象就是实例化的一个Date对象

我写了一个关于bind的分析,我觉得this instanceOf fBound也没有问题,能不能帮我看一下有什么遗漏的地方
https://blog.csdn.net/Metropo...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题