一、this
this的5种绑定规则
- 1、默认绑定(严格/非严格模式)
- 2、隐式绑定
- 3、显式绑定
- 4、new绑定
- 5、箭头函数绑定 (根据外层作用域判断)
1.2 => 谁调用就绑定谁的this
3 => call apply bind
https://mp.weixin.qq.com/s/hYm0JgBI25grNG_2sCRlTA
二、apply call
https://github.com/mqyqingfeng/Blog/issues/11
call() 方法调用一个函数, 其具有一个指定的
this值和分别地提供的参数(参数的列表)。
call apply区别在于call()方法接受的是若干个参数的列表,而apply()方法接受的是一个包含多个参数的数组
举个🌰
var func = function(arg1, arg2) {...};func.call(this, arg1, arg2); // 使用 call,参数列表func.apply(this, [arg1, arg2]) // 使用 apply,参数数组
call模拟实现
第一步: 基本框架
/*** 1.将函数设为对象的属性* 2.执行该函数* 3.删除该函数*/Function.prototype.call2 = function (context){// 首先要获取调用call的函数,用this可以获取context.fn = thiscontext.fn()delete context.fn}// testvar foo = {name: 'june'}function bar() {console.log(this)console.log(this.name)}bar.call2(foo)
第二步:传递参数
Function.prototype.call2 = function (context){// 首先要获取调用call的函数,用this可以获取context.fn = this// 处理 argumentsvar args = []for (var i = 1; i < arguments.length; i++) {args.push('arguments[' + i + ']');}console.log(args);// 执行函数eval('context.fn(' + args +')')delete context.fn}var foo = {value: 'june'}function bar(a1, a2) {console.log(this)console.log(a1)console.log(a2)console.log(this.value)}bar.call2(foo, 'a1', 'a2')
第三步:兼容null 和 函数 return
Function.prototype.call2 = function (context) {// 当为 null 的时候,视为指向 windowvar context = context || window;context.fn = this;var args = [];for(var i = 1, len = arguments.length; i < len; i++) {args.push('arguments[' + i + ']');}var result = eval('context.fn(' + args +')');delete context.fn// 函数是可以有返回值的return result;}
apply 的实现跟 call 类似,只是在处理参数的时候,针对数组做处理
Function.prototype.apply = function (context, arr) {var context = Object(context) || window;context.fn = this;var result;if (!arr) {result = context.fn();}else {var args = [];for (var i = 0, len = arr.length; i < len; i++) {args.push('arr[' + i + ']');}result = eval('context.fn(' + args + ')')}delete context.fnreturn result;}
三、bind
https://github.com/mqyqingfeng/Blog/issues/12
bind() 方法会创建一个新函数。当这个新函数被调用时,bind() 的第一个参数将作为它运行时的 this,之后的一序列参数将会在传递的实参前传入作为它的参数。(来自于 MDN )
由此我们可以首先得出 bind 函数的两个特点:
- 返回一个函数
- 可以传入参数
bind使用举例
var foo = {value: 1};function bar() {console.log(this.value);}// 返回了一个函数var bindFoo = bar.bind(foo);bindFoo(); // 1
第一步:返回绑定this的函数
Function.prototype.bind2 = function (context) {var _this = this// 返回函数return function () {// 绑定this,并兼容可能有的返回值return _this.apply(context)}}
第二步:传递参数
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);return self.apply(context, args.concat(bindArgs));}}
第三步:构造函数实现
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 指向实例,此时结果为 true,将绑定函数的 this 指向该实例,可以让实例获得来自绑定函数的值// 以上面的是 demo 为例,如果改成 `this instanceof fBound ? null : context`,实例只是一个空对象,将 null 改成 this ,实例会具有 habit 属性// 当作为普通函数时,this 指向 window,此时结果为 false,将绑定函数的 this 指向 contextreturn self.apply(this instanceof fBound ? this : context, args.concat(bindArgs));}// 修改返回函数的 prototype 为绑定函数的 prototype,实例就可以继承绑定函数的原型中的值fBound.prototype = this.prototype;return fBound;}
但是在这个写法中,我们直接将 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);return self.apply(this instanceof fNOP ? this : context, args.concat(bindArgs));}fNOP.prototype = this.prototype;fBound.prototype = new fNOP();return fBound;}
