博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript之实现bind
阅读量:6937 次
发布时间:2019-06-27

本文共 2766 字,大约阅读时间需要 9 分钟。

前言

bind函数的主要特性:

  • 创建一个新的绑定函数,这个函数与被调用的函数具有相同的函数体
  • 当这个新函数被调用的时候,函数中this 指向 bind 方法中第一个参数
  • 可以传递参数,bind 方法中传递的参数会在绑定函数实参之前

正文

实现bind函数

1、实现绑定指定this与传递参数

'use strict';        Function.prototype.mybind = function(context) {        var _this = this;        var outerArgs = Array.prototype.slice.call(arguments, 1);        return function() {            var innerArgs = Array.prototype.slice.call(arguments);            return _this.apply(context, outerArgs.concat(innerArgs));        }    }复制代码

2、当把返回的函数当作构造函数的时候,bind方法中第一个参数会被忽略(即不会绑定this)。

'use strict';        Function.prototype.mybind = function(context) {        var _this = this;        var outerArgs = Array.prototype.slice.call(arguments, 1);        var BoundFn = function() {            var innerArgs = Array.prototype.slice.call(arguments);            // 当此时Fn函数返回出去,被当作构造函数,用new操作符调用的时候,this指向Fn的实例            return _this.apply(this instanceof BoundFn ? this : context, outerArgs.concat(innerArgs));        }        // 把当前函数的prototype赋值给返回函数的prototype        BoundFn.prototype = this.prototype;        return BoundFn;    } 复制代码

3、上面的代码虽然bind功能实现了,但是存在一个问题,当改变BoundFn函数实例的原型的时候,会影响到原函数的原型,相反也一样,两者的原型是同一个引用,所以需要完善一下。

'use strict';        Function.prototype.mybind = function(context) {        if(typeof this !== 'function') {            throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');        };        var _this = this;        var outerArgs = Array.prototype.slice.call(arguments, 1);        var BoundFn = function() {            var innerArgs = Array.prototype.slice.call(arguments);            // 当此时BoundFn函数返回出去,被当作构造函数,用new操作符调用的时候,this指向BoundFn的实例            return _this.apply(this instanceof BoundFn ? this : context, outerArgs.concat(innerArgs));        };        var Fn = function() {};        fn.prototype = this.prototype;        BoundFn.prototype = new Fn();        return BoundFn;    }复制代码

此时bind函数已经实现了,bind函数在 ECMA-262 第五版才被加入,它可能无法在所有浏览器上运行,所以需要做下兼容:

if(!Function.prototype.bind) {        Function.prototype.bind = function(context) {            if(typeof this !== 'function') {                throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');            };            var _this = this;            var outerArgs = Array.prototype.slice.call(arguments, 1);            var BoundFn = function() {                var innerArgs = Array.prototype.slice.call(arguments);                // 当此时Fn函数返回出去,被当作构造函数,用new操作符调用的时候,this指向Fn的实例                return _this.apply(this instanceof BoundFn ? this : context, outerArgs.concat(innerArgs));            };            var Fn = function() {};            fn.prototype = this.prototype;            BoundFn.prototype = new Fn();            return BoundFn;        }    }复制代码

转载地址:http://mybnl.baihongyu.com/

你可能感兴趣的文章
Android 屏幕自适应方向尺寸
查看>>
java 泛型好文收集
查看>>
Eclipse使用技巧总结(四)——代码重构专题
查看>>
如何将iOS应用发布到App Store详解
查看>>
MyEclipse 编码设置
查看>>
Tcp
查看>>
Shell基本知识
查看>>
POJ 1488
查看>>
删除Azure Active Directory
查看>>
ReLu(Rectified Linear Units)激活函数
查看>>
黑马程序员---java基础-----------------图形化界面(GUI)
查看>>
DB2创建数据库常用参数详解
查看>>
使用sql追踪
查看>>
5: EL 表达式小结
查看>>
[数组]数组元素分割
查看>>
今日学习20190427
查看>>
HIbernate小结
查看>>
iOS开发-数据存储
查看>>
iOSUI-UIScrollView属性,方法大全
查看>>
2015大连华信校园招聘面试题--堆栈
查看>>