在实现本身的call
,apply
,bind
前,须要复习一下this
.
this
其实能够理解成一根指针:其实 this 的指向,始终坚持一个原理:this
永远指向最后调用它的那个对象,这就是精髓。最关键所在前端
this
的四种指向:当this
所在的函数被普通调用时,指向window
,若是当前是严格模式,则指向undefined
web
function test() { console.log(this); }; test(); 指向window 输出下面的代码: // Window {speechSynthesis: SpeechSynthesis, caches: CacheStorage, localStorage: Storage, sessionStorage: Storage, webkitStorageInfo: DeprecatedStorageInfo…}
严格模式 'use strict'; function test() { console.log(this); }; test(); // undefined
当this
所在当函数被以obj.fn()
形式调用时,指向obj
数组
var obj = { name: 'segmentFault', foo: function() { console.log(this.name); } } obj.foo(); // 'segmentFault'
还能够这么作session
function test() { console.log(this.name); } var obj = { name: 'qiutc', foo: test } obj.foo(); // 'qiutc'
当call,apply
加入后,this
的指向被改变了app
function a(a,b,c) { console.log(this.name); console.log(a,b,c) } const b = { name: "segmentFault" } a.call(b,1,2,3) //输出 segmentFault和 1,2,3 function a(a,b,c) { console.log(this.name); console.log(a,b,c) } a.apply(b,[1,2,3]) //输出segmentFault和1,2,3
遇到bind后 :框架
function a() { console.log(this.name); } const b = { name: "segmentFault" } a.bind(b, 1, 2, 3)
此时控制台并无代码输出,由于bind会从新生成而且返回一个函数,这个函数的this
指向第一个参数koa
function a() { console.log(this.name); } const b = { name: "segmentFault" } const c = a.bind(b, 1, 2, 3) c() //此时输出segmentFault
call
:在函数原型上定义本身的myCall
方法:函数
Function.prototype.myCall = function (context, ...arg) { const fn = Symbol('临时属性') context[fn] = this context[fn](...arg) delete context[fn] }
四行代码实现了简单的call
,思路以下:学习
this
指向这个对象symbol
属性,调用完毕删除symbol
属性就是调用mycall
方法的函数...arg
是将多个形参都塞到一个数组里,在函数内部使用arg这个变量时,就是包含全部形参的数组 context[fn](...arg)
时候,...arg
是为了展开数组,依次传入参数调用函数为了简化,今天都不作类型判断和错误边际处理,只把原理讲清楚。
apply
在函数原型上定义本身的myApply
方法:this
//实现本身的myApply Function.prototype.myApply = function (context, arg) { const fn = Symbol('临时属性') context[fn] = this context[fn](...arg) delete context[fn] } const obj2 = { a: 1 } test.myApply(obj2, [2, 3, 4])
同理,只是apply
传递的第二个参数是数组,这里咱们只须要在调用时,将参数用...
把数组展开便可
bind
:bind
跟apply,call
的本质区别,bind
不会改变原函数的this
指向,只会返回一个新的函数(咱们想要的那个this
指向),而且不会调用。可是apply
和call
会改变原函数的this
指向而且直接调用
bind
在编写框架源码,例如koa
等中用得特别多:
//实现本身的myBind Function.prototype.myBind = function (context, ...firstarg) { const that = this const bindFn = function (...secoundarg) { return that.myCall(context, ...firstarg, ...secoundarg) } bindFn.prototype = Object.create(that.prototype) return bindFn } var fnbind = test.myBind(obj, 2) fnbind(3)
同理 本身定义好原型上的myBind
方法this
劫持 保留最初的调用mybind
方法的那个对象
返回一个新的函数 这个新的函数内部this
指向已经肯定,使用的是咱们的mycall
方法
学习须要按部就班,建议根据本文顺序去封装一遍,是比较轻松的,固然bind
还须要判断是不是new
调用.
bind
Function.prototype.myBind = function (objThis, ...params) { const thisFn = this; // 存储源函数以及上方的params(函数参数) // 对返回的函数 secondParams 二次传参 let fToBind = function (...secondParams) { console.log('secondParams',secondParams,...secondParams) const isNew = this instanceof fToBind // this是不是fToBind的实例 也就是返回的fToBind是否经过new调用 const context = isNew ? this : Object(objThis) // new调用就绑定到this上,不然就绑定到传入的objThis上 return thisFn.call(context, ...params, ...secondParams); // 用call调用源函数绑定this的指向并传递参数,返回执行结果 }; fToBind.prototype = Object.create(thisFn.prototype); // 复制源函数的prototype给fToBind return fToBind; // 返回拷贝的函数 };
以为写得不错能够给个
star
,欢迎加入咱们的前端交流群~:
如今人数超过了100人,因此只能加我,而后拉大家进群!!
..]