一、bind
1、基本概念
**bind()** 方法创建一个新的函数,在 bind() 被调用时,这个新函数的 this 被指定为 bind() 的第一个参数,而其余参数将作为新函数的参数,供调用时使用。 — MDN
2、手写实现
// 第一种不支持使用new调用新创建的构造函数,而第二种支持// Does not work with `new (funcA.bind(thisArg, args))`if (!Function.prototype.bind) (function(){var slice = Array.prototype.slice;Function.prototype.bind = function() {var thatFunc = this, thatArg = arguments[0];var args = slice.call(arguments, 1);if (typeof thatFunc !== 'function') {// closest thing possible to the ECMAScript 5// internal IsCallable functionthrow new TypeError('Function.prototype.bind - ' +'what is trying to be bound is not callable');}return function(){var funcArgs = args.concat(slice.call(arguments))return thatFunc.apply(thatArg, funcArgs);};};})();// Yes, it does work with `new (funcA.bind(thisArg, args))`if (!Function.prototype.bind) (function(){var ArrayPrototypeSlice = Array.prototype.slice;Function.prototype.bind = function(otherThis) {if (typeof this !== 'function') {// closest thing possible to the ECMAScript 5// internal IsCallable functionthrow new TypeError('Function.prototype.bind - what is trying to be bound is not callable');}var baseArgs= ArrayPrototypeSlice.call(arguments, 1),baseArgsLength = baseArgs.length,fToBind = this,fNOP = function() {},fBound = function() {baseArgs.length = baseArgsLength; // reset to default base argumentsbaseArgs.push.apply(baseArgs, arguments);return fToBind.apply(fNOP.prototype.isPrototypeOf(this) ? this : otherThis, baseArgs);};if (this.prototype) {// Function.prototype doesn't have a prototype propertyfNOP.prototype = this.prototype;}fBound.prototype = new fNOP();return fBound;};})();
二、call
1、基本概念
**call()** 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。
注意:该方法的语法和作用与
apply()方法类似,只有一个区别,就是 call() 方法接受的是一个参数列表,而 apply() 方法接受的是一个包含多个参数的数组。
2、手写实现
Function.prototype.call = function(context = window, ...args) {if (typeof this !== 'function') {throw new TypeError('Type Error');}const fn = Symbol('fn');context[fn] = this;const res = context[fn](...args);delete context[fn];return res;}
三、aplly
1、基本概念
**apply()** 方法调用一个具有给定this值的函数,以及以一个数组(或类数组对象)的形式提供的参数。
注意:call()方法的作用和 apply() 方法类似,区别就是call()方法接受的是参数列表,而apply()方法接受的是一个参数数组
2、手写实现
Function.prototype.apply = function(context = window, args) {if (typeof this !== 'function') {throw new TypeError('Type Error');}const fn = Symbol('fn');context[fn] = this;const res = context[fn](...args);delete context[fn];return res;}
