if(!Function.prototype.bind){

  Function.prototype.bind = function(oThis){

    if(typeof this !=="function"){ //如果不函数抛出异常

      throw new TyperError("")

    }

    var aArgs = Array.prototype.slice.call(arguments,1),   //此处的aArgs是除函数外的参数

      fToBind = this,                  //要绑定的对象

      fNOP = function(){},

      fBound = function(){

        return fToBind.apply(

          this instanceof fNOP ? this:oThis||this,aArgs.concat(Array.prototype.slice.call(arguments)));

          )

      };

    fNOP.prototype = this.prototype;

    fBound.prototype = new fNOP();

    return  fBound;

  }

}

明白 bind 的用法就必须要知道 apply 的用法,MDN 指出,apply 是直接修改了函数内部的指向到第一个参数,并将第二个参数数组传参进函数并运行这个函数。也就是说

var obj = {test: function() { console.log(this, arguments) }},
func = obj.test; obj.test("Hello", ",", "world", "!");
func.apply(obj, ["Hello", ",", "world", "!"]);

这两种运行方式是一样的。那么回到 Polyfill 中发现参数的写法是 args.concat(slice.call(arguments))args 是将 bind时候定义的除第一个参数外的其它参数,而此时的 arguments 是指函数调用时候的参数,通过数组的操作将这两个参数合并成一个数组传入函数内部。看个例子你可能更容易明白:

/** 代码接上 **/
var newFunc = func.bind(obj, "Hello", ",");
newFunc("world", "!");

那么再来回答问题一,这个是典型的属性继承的方法,本来使用

bound.prototype = self.prototype

就可以将原属性集成过来了,但是这样两个对象属性都指向同一个地方,修改 bound.prototype 将会造成 self.prototype也发生改变,这样并不是我们的本意。所以通过一个空函数 nop 做中转,能有效的防止这种情况的发生。

bind返回的是函数

if (!Function.prototype.bind) {
Function.prototype.bind = function(obj) {
var _self = this
,args = arguments;
return function() {
_self.apply(obj, Array.prototype.slice.call(args, 1));
}
}
}

手写Function.bind函数的更多相关文章

  1. 依据ECMA规范,手写一个bind函数

    Function.prototype.bind 函数,参见ECMA规范地址 如题,这次来实现一个boundFunction函数,不挂载在Function.prototype上,而是一个单独声明的函数. ...

  2. 手写简化版printf函数

    2019.02.01更新:经同学提醒,myprintf函数应有返回值为输出的字符数. 期末的大作业,手写一个myprintf函数,支持如下一些操作. 也就是  % -(负号控制左右对齐) 数(控制字段 ...

  3. 手写事件代理函数 (Delegated function)

    ‘手写 ’ 这个词 ,面试是不是听过无数遍呢 ! 今天我们来手写一个这样的事件委托函数 => function( parent, selector, type ,  handle)  {} 你需 ...

  4. 前端面试手写代码——JS函数柯里化

    目录 1 什么是函数柯里化 2 柯里化的作用和特点 2.1 参数复用 2.2 提前返回 2.3 延迟执行 3 封装通用柯里化工具函数 4 总结和补充 1 什么是函数柯里化 在计算机科学中,柯里化(Cu ...

  5. 【OpenCV学习笔记】之六 手写图像旋转函数---万丈高楼平地起

    话说,平凡之处显真格,这一点也没错!  比如,对旋转图像进行双线性插值,很简单吧?  可,对我,折腾了大半天,也没有达到预期效果!  尤其是三个误区让我抓瞎好久: 1,坐标旋转公式.   这东西,要用 ...

  6. cs224d 作业 problem set2 (一) 用tensorflow纯手写实现sofmax 函数,线性判别分析,命名实体识别

    Hi Dear Today we will use tensorflow to implement the softmax regression and linear classifier algor ...

  7. 手写map, filter函数

    function map(arr, fn) { let newArr = []; for (let i = 0; i < arr.length; i++) { newArr[i] = fn(ar ...

  8. 手写一个bind

    1 Function.prototype.bind1 = function(){ 2 // 将类数组转化成数组 3 let arr = Array.prototype.slice.call(argum ...

  9. 前端面试手写代码——call、apply、bind

    1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...

随机推荐

  1. PAT甲级——A1109 Group Photo【25】

    Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...

  2. IDEA 安装lombok及使用

    1.File-Settings-Plugins-Brows Repositories-输入lombok-install 2.重启idea 3.添加maven依赖 <dependency> ...

  3. JVM 内存模型及垃圾回收

    java内存模型 根据 JVM 规范,JVM 内存共分为虚拟机栈.堆.方法区.程序计数器.本地方法栈五个部分. 程序计数器:程序计数器是指CPU中的寄存器,它保存的是程序当前执行的指令的地址(也可以说 ...

  4. Hive学习笔记简版

    一.概述 1. Hive是Apache提供的基于Hadoop的数据仓库管理工具2. Hive提供了类SQL语言来操作Hadoop,底层会将SQL转化为MapReduce来执行,所以效率会比较低3. H ...

  5. 2019-2-17-如何在-Windows-10-中移除-Internet-Explorer-浏览器

    title author date CreateTime categories 如何在 Windows 10 中移除 Internet Explorer 浏览器 lindexi 2019-02-17 ...

  6. android 中的一些小问题

    1 TextView 在TableRow 中占满一行 要为TextView设置 android:layout_weight="1" 这个属性 2

  7. 防范永恒之蓝勒索病毒-XP、Win10文件共享怎样设置

    企业内部员工之间的文件共享,是企业内部文件交换的重要手段.传统的文件共享是通过Windows的目录共享来实现的,而目录共享功能因其可能存在安全隐患使得很多企业分发放弃了这个文件共享模式. 如去年勒索病 ...

  8. jmeter参数化遇到的问题

    遇到的问题是点击运行后,察看结果树没有任何结果,且右上角的警告日志是: meter.threads.JMeterThread: Test failed! java.lang.IllegalArgume ...

  9. ElasticSearch _bulk批量处理报错The bulk request must be terminated by a newline

    在JSON数据最后回车换行,代码中可以

  10. sql自定义日期函数,返回范围内日期和星期数表。

    Create function [dbo].[FUN_GenerateTime] ( @begin_date datetime, -- 起始时间 @end_date datetime -- 结束时间 ...