手写JS函数的call、apply、bind实现_java

  

     之所以要写这篇,是因为曾经面试被要求在白纸上手写bind实现

  结果跟代码一样清晰明确,一阵懵逼,没写出来!

  下面,撸起袖子就是干!~

  把call、apply、bind一条龙都整一遍!~~

call

定义与使用

Function.prototype.call(): https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/call

  1. // Function.prototype.call()样例

  2. function fun(arg1, arg2) {

  3. console.log(this.name)

  4. console.log(arg1 + arg2)

  5. }

  6. const _this = { name: 'YIYING' }

  7. // 接受的是一个参数列表;方法立即执行

  8. fun.call(_this, 1, 2)

  1. // 输出:

  2. YIYING

  3. 3

手写实现

  1. /**

  2. * 自定义call实现

  3. * @param context 上下文this对象

  4. * @param args 动态参数

  5. */

  6. Function.prototype.ownCall = function(context, ...args) {

  7. context = (typeof context === 'object' ? context : window)

  8. // 防止覆盖掉原有属性

  9. const key = Symbol()

  10. // 这里的this为需要执行的方法

  11. context[key] = this

  12. // 方法执行

  13. const result = context[key](...args)

  14. delete context[key]

  15. return result

  16. }

  1. // 验证样例

  2. function fun(arg1, arg2) {

  3. console.log(this.name)

  4. console.log(arg1 + arg2)

  5. }

  6. const _this = { name: 'YIYING' }

  7. // 接受的是一个参数列表;方法立即执行

  8. fun.ownCall(_this, 1, 2)

  1. // 输出:

  2. YIYING

  3. 3

apply

定义与使用

Function.prototype.apply(): https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

  1. // Function.prototype.apply()样例

  2. function fun(arg1, arg2) {

  3. console.log(this.name)

  4. console.log(arg1 + arg2)

  5. }

  6. const _this = { name: 'YIYING' }

  7. // 参数为数组;方法立即执行

  8. fun.apply(_this, [1, 2])

  1. // 输出:

  2. YIYING

  3. 3

手写实现

  1. /**

  2. * 自定义Apply实现

  3. * @param context 上下文this对象

  4. * @param args 参数数组

  5. */

  6. Function.prototype.ownApply = function(context, args) {

  7. context = (typeof context === 'object' ? context : window)

  8. // 防止覆盖掉原有属性

  9. const key = Symbol()

  10. // 这里的this为需要执行的方法

  11. context[key] = this

  12. // 方法执行

  13. const result = context[key](...args)

  14. delete context[key]

  15. return result

  16. }

  1. // 验证样例

  2. function fun(arg1, arg2) {

  3. console.log(this.name)

  4. console.log(arg1 + arg2)

  5. }

  6. const _this = { name: 'YIYING' }

  7. // 参数为数组;方法立即执行

  8. fun.ownApply(_this, [1, 2])

  1. // 输出:

  2. YIYING

  3. 3

bind

定义与使用

Function.prototype.bind() : https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

  1. // Function.prototype.bind()样例

  2. function fun(arg1, arg2) {

  3. console.log(this.name)

  4. console.log(arg1 + arg2)

  5. }

  6. const _this = { name: 'YIYING' }

  7. // 只变更fun中的this指向,返回新function对象

  8. const newFun = fun.bind(_this)

  9. newFun(1, 2)

  1. // 输出:

  2. YIYING

  3. 3

手写实现

  1. /**

  2. * 自定义bind实现

  3. * @param context 上下文

  4. * @returns {Function}

  5. */

  6. Function.prototype.ownBind = function(context) {

  7. context = (typeof context === 'object' ? context : window)

  8. return (...args)=>{

  9. this.call(context, ...args)

  10. }

  11. }

  1. // 验证样例

  2. function fun(arg1, arg2) {

  3. console.log(this.name)

  4. console.log(arg1 + arg2)

  5. }

  6. const _this = { name: 'YIYING' }

  7. // 只变更fun中的this指向,返回新function对象

  8. const newFun = fun.ownBind(_this)

  9. newFun(1, 2)

  1. // 输出:

  2. YIYING

  3. 3

最后,麻烦以后面试不要再考这道题了!~~~

https://mp.weixin.qq.com/s/vQlZhCfyfRFEY2u31qbGtg