热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

JavaScript深切之bind的模仿完成

JavaScript深切系列第十一篇,经由过程bind函数的模仿完成,带人人真正相识bind的特征bind一句话引见bind:bind()方法会建立一个新函数。当这个新函数被挪用时

Javascript深切系列第十一篇,经由过程bind函数的模仿完成,带人人真正相识bind的特征

bind

一句话引见 bind:

bind() 方法会建立一个新函数。当这个新函数被挪用时,bind() 的第一个参数将作为它运行时的 this,以后的一序列参数将会在通报的实参前传入作为它的参数。(来自于 MDN )

由此我们能够起首得出 bind 函数的两个特性:

  1. 返回一个函数

  2. 能够传入参数

返回函数的模仿完成

从第一个特性最先,我们举个例子:

var foo = {
value: 1
};
function bar() {
console.log(this.value);
}
// 返回了一个函数
var bindFoo = bar.bind(foo);
bindFoo(); // 1

关于指定 this 的指向,我们能够运用 call 或许 apply 完成,关于 call 和 apply 的模仿完成,能够检察《Javascript深切之call和apply的模仿完成》。我们来写初版的代码:

// 初版
Function.prototype.bind2 = function (context) {
var self = this;
return function () {
self.apply(context);
}
}

传参的模仿完成

接下来看第二点,能够传入参数。这个就有点让人费解了,我在 bind 的时刻,是不是能够传参呢?我在实行 bind 返回的函数的时刻,可不能够传参呢?让我们看个例子:

var foo = {
value: 1
};
function bar(name, age) {
console.log(this.value);
console.log(name);
console.log(age);
}
var bindFoo = bar.bind(foo, 'daisy');
bindFoo('18');
// 1
// daisy
// 18

函数须要传 name 和 age 两个参数,居然还能够在 bind 的时刻,只传一个 name,在实行返回的函数的时刻,再传另一个参数 age!

这可咋办?不急,我们用 arguments 举行处置惩罚:

// 第二版
Function.prototype.bind2 = function (context) {
var self = this;
// 猎取bind2函数从第二个参数到末了一个参数
var args = Array.prototype.slice.call(arguments, 1);
return function () {
// 这个时刻的arguments是指bind返回的函数传入的参数
var bindArgs = Array.prototype.slice.call(arguments);
self.apply(context, args.concat(bindArgs));
}
}

组织函数效果的模仿完成

完成了这两点,最难的部分到啦!由于 bind 另有一个特性,就是

一个绑定函数也能运用new操作符建立对象:这类行动就像把原函数当做组织器。供应的 this 值被疏忽,同时挪用时的参数被供应给模仿函数。

也就是说当 bind 返回的函数作为组织函数的时刻,bind 时指定的 this 值会失效,但传入的参数依旧见效。举个例子:

var value = 2;
var foo = {
value: 1
};
function bar(name, age) {
this.habit = 'shopping';
console.log(this.value);
console.log(name);
console.log(age);
}
bar.prototype.friend = 'kevin';
var bindFoo = bar.bind(foo, 'daisy');
var obj = new bindFoo('18');
// undefined
// daisy
// 18
console.log(obj.habit);
console.log(obj.friend);
// shopping
// kevin

注重:只管在全局和 foo 中都声清楚明了 value 值,末了依旧返回了 undefind,申明绑定的 this 失效了,假如人人相识 new 的模仿完成,就会晓得这个时刻的 this 已指向了 obj。

(哈哈,我这是为我的下一篇文章《Javascript深切系列之new的模仿完成》打广告)。

所以我们能够经由过程修正返回的函数的原型来完成,让我们写一下:

// 第三版
Function.prototype.bind2 = function (context) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
var fbound = function () {
var bindArgs = Array.prototype.slice.call(arguments);
// 当作为组织函数时,this 指向实例,self 指向绑定函数,由于下面一句 `fbound.prototype = this.prototype;`,已修正了 fbound.prototype 为 绑定函数的 prototype,此时效果为 true,当效果为 true 的时刻,this 指向实例。
// 当作为一般函数时,this 指向 window,self 指向绑定函数,此时效果为 false,当效果为 false 的时刻,this 指向绑定的 context。
self.apply(this instanceof self ? this : context, args.concat(bindArgs));
}
// 修正返回函数的 prototype 为绑定函数的 prototype,实例就能够继续函数的原型中的值
fbound.prototype = this.prototype;
return fbound;
}

假如对原型链稍有疑心,能够检察《Javascript深切之从原型到原型链》。

组织函数效果的优化完成

但是在这个写法中,我们直接将 fbound.prototype = this.prototype,我们直接修正 fbound.prototype 的时刻,也会直接修正函数的 prototype。这个时刻,我们能够经由过程一个空函数来举行中转:

// 第四版
Function.prototype.bind2 = function (context) {
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
var fNOP = function () {};
var fbound = function () {
var bindArgs = Array.prototype.slice.call(arguments);
self.apply(this instanceof self ? this : context, args.concat(bindArgs));
}
fNOP.prototype = this.prototype;
fbound.prototype = new fNOP();
return fbound;
}

到此为止,大的题目都已处理,给本身一个赞!o( ̄▽ ̄)d

三个小题目

接下来处置惩罚些小题目:

1.apply 这段代码跟 MDN 上的稍有差别

在 MDN 中文版讲 bind 的模仿完成时,apply 这里的代码是:


self.apply(this instanceof self ? this : context || this, args.concat(bindArgs))

多了一个关于 context 是不是存在的推断,但是这个是毛病的!

举个例子:

var value = 2;
var foo = {
value: 1,
bar: bar.bind(null)
};
function bar() {
console.log(this.value);
}
foo.bar() // 2

以上代码一般情况下会打印 2,假如换成了 context || this,这段代码就会打印 1!

所以这里不应该举行 context 的推断,人人检察 MDN 一样内容的英文版,就不存在这个推断!

2.挪用 bind 的不是函数咋办?

不可,我们要报错!

if (typeof this !== "function") {
throw new Error("Function.prototype.bind - what is trying to be bound is not callable");
}

3.我要在线上用

那别忘了做个兼容:

Function.prototype.bind = Function.prototype.bind || function () {
……
};

固然最好是用es5-shim啦。

终究代码

所以最末了的代码就是:

Function.prototype.bind2 = function (context) {
if (typeof this !== "function") {
throw new Error("Function.prototype.bind - what is trying to be bound is not callable");
}
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
var fNOP = function () {};
var fbound = function () {
self.apply(this instanceof self ? this : context, args.concat(Array.prototype.slice.call(arguments)));
}
fNOP.prototype = this.prototype;
fbound.prototype = new fNOP();
return fbound;
}

下一篇文章

《Javascript深切系列之new的模仿完成》

相干链接

《Javascript深切之从原型到原型链》

《Javascript深切之call和apply的模仿完成》

《Javascript深切系列之new的模仿完成》

深切系列

Javascript深切系列目次地点:https://github.com/mqyqingfeng/Blog。

Javascript深切系列估计写十五篇摆布,旨在帮人人捋顺Javascript底层学问,重点解说如原型、作用域、实行上下文、变量对象、this、闭包、按值通报、call、apply、bind、new、继续等难点观点。

假如有毛病或许不严谨的处所,请务必赋予斧正,非常谢谢。假如喜好或许有所启示,迎接star,对作者也是一种勉励。


推荐阅读
author-avatar
KeNnyT0nGMM_934
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有