站点工具

用户工具


手写bind、call、apply

bind、call、apply都是用来指定一个函数内部的this的值, 先看看bind、call、apply的用法

var year = 2021
function getDate(month, day) {
  return this.year + '-' + month + '-' + day
}
 
let obj = {year: 2022}
getDate.call(null, 3, 8)    //2021-3-8
getDate.call(obj, 3, 8)     //2022-3-8
getDate.apply(obj, [6, 8])  //2022-6-8
getDate.bind(obj)(3, 8)     //2022-3-8

实现call

Function.prototype.call2 = function(context, ...args) {
  context = (context === undefined || context === null) ? window : context
  context.__fn = this
  let result = context.__fn(...args)
  delete context.__fn
  return result
}

实现apply

Function.prototype.apply2 = function(context, args) {
  context = (context === undefined || context === null) ? window : context
  context.__fn = this
  let result = context.__fn(...args)
  delete context.__fn
  return result
}

实现bind

Function.prototype.bind2 = function(context, ...args1) {
  context = (context === undefined || context === null) ? window : context
  let _this = this
  return function(...args2) {
    context.__fn = _this
    let result = context.__fn(...[...args1, ...args2])
    delete context.__fn
    return result
  }
}

问题:如果绑定的对象不允许新增属性怎么办?

若愚 · 2021/09/23 11:26 · javascript_手写call.txt