javascript - bind函数polyfill源码
迷茫
迷茫 2017-04-11 11:24:50
[JavaScript讨论组]
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是什么意思,能举个例子说明吗,多谢?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(2)
PHP中文网

移步 https://github.com/hanzichi/u... 以前写过 bind 的文章

也可以看看这篇 https://github.com/hanzichi/u...

ringa_lee

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

var time=new Date();
time instanceof Date;//true,time对象就是实例化的一个Date对象
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号