javascript中new,call,apply,bind等方法是咱们常常要使用到,在伪数组转数组、函数传参、继承等场景中,都离不开他们。这里就不具体讨论他们的使用方法,咱们将研究他们的实现方式,重写属于咱们本身的方法,让你们从新认识他们的实现过程,使咱们在开发中知其然,知其因此然!javascript
咱们用new实例化一个构造函数,生成一个实例对象,而new到底作了什么呢,主要分为如下五步:java
function MyNew() { let Constructor = Array.prototype.shift.call(arguments); // 1:取出构造函数 let obj = {} // 2:执行会建立一个新对象 obj.__proto__ = Constructor.prototype // 3:该对象的原型等于构造函数prototype var result = Constructor.apply(obj, arguments) // 4: 执行函数中的代码 return typeof result === 'object' ? result : obj // 5: 返回的值必须为对象 }
function Man(name, age) { this.name = name this.age = age } var tom = new Man('tom', 20) var mike = MyNew(Man, 'mike', 30) console.log(tom instanceof Man, mike instanceof Man) // true true
call方法的实现主要有如下三步,好比 fn.call(obj, a, b)
:数组
Function.prototype.myCall = function (context) { context = context ? Object(context) : window context.fn = this // 重置上下文 let args = [...arguments].slice(1) // 截取参数a,b let r = context.fn(...args) // 执行函数 delete context.fn // 删除属性,避免污染 return r // 返回结果 }
// 浏览器环境下 var a = 1, b = 2; var obj ={a: 10, b: 20} function test(key1, key2){ console.log(this[key1] + this[key2]) } test('a', 'b') // 3 test.myCall(obj, 'a', 'b') // 30
apply方法和call方法大同小异,惟一差异就是,apply传入的参数是数组格式。浏览器
// apply 原理 Function.prototype.myApply = function (context) { context = context ? Object(context) : window context.fn = this let args = [...arguments][1] if (!args) { return context.fn() } let r = context.fn(...args) delete context.fn; return r }
// 浏览器环境下 var a = 1, b = 2; var obj ={a: 10, b: 20} function test(key1, key2){ console.log(this[key1] + this[key2]) } test('a', 'b') // 3 test.myCall(obj, ['a', 'b']) // 30 注意这里是传入数组 ['a', 'b']
bind方法和call、apply方法的差异是,他们都改变了上下文,可是bind没有当即执行函数。app
// bind 原理 Function.prototype.Mybind = function (context) { let _me = this return function () { return _me.apply(context) } }
var a = 1, b = 2; var obj ={a: 10, b: 20} function test(key1, key2){ console.log(this[key1] + this[key2]) } var fn = test.bind(obj) fn('a', 'b') // 30
好了,介绍完了,若是以为对你有帮助,点个赞哈,嘿嘿!函数