前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >手写JS函数的call、apply、bind

手写JS函数的call、apply、bind

原创
作者头像
helloworld1024
发布2022-10-26 12:56:26
1.2K0
发布2022-10-26 12:56:26
举报

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

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

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

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

call

定义与使用

Function.prototype.call(): developer.mozilla.org/zh-CN/docs/…

代码语言:text
复制
// Function.prototype.call()样例
function fun(arg1, arg2) {
  console.log(this.name)
  console.log(arg1 + arg2)
}
const _this = { name: 'YIYING' }
// 接受的是一个参数列表;方法立即执行
fun.call(_this, 1, 2)
代码语言:text
复制
// 输出:
YIYING
3
手写实现
代码语言:javascript
复制
/** * 自定义call实现 * @param context   上下文this对象 * @param args      动态参数 */
Function.prototype.ownCall = function(context, ...args) {
  context = (typeof context === 'object' ? context : window)
  // 防止覆盖掉原有属性
  const key = Symbol()
  // 这里的this为需要执行的方法
  context[key] = this
  // 方法执行
  const result = context[key](...args)
  delete context[key]
  return result
}
代码语言:text
复制
// 验证样例
function fun(arg1, arg2) {
  console.log(this.name)
  console.log(arg1 + arg2)
}
const _this = { name: 'YIYING' }
// 接受的是一个参数列表;方法立即执行
fun.ownCall(_this, 1, 2)
代码语言:text
复制
// 输出:
YIYING
3

apply

定义与使用

Function.prototype.apply(): developer.mozilla.org/zh-CN/docs/…

代码语言:text
复制
// Function.prototype.apply()样例
function fun(arg1, arg2) {
  console.log(this.name)
  console.log(arg1 + arg2)
}
const _this = { name: 'YIYING' }
// 参数为数组;方法立即执行
fun.apply(_this, [1, 2])
代码语言:text
复制
// 输出:
YIYING
3
手写实现
代码语言:javascript
复制
/** * 自定义Apply实现 * @param context   上下文this对象 * @param args      参数数组 */
Function.prototype.ownApply = function(context, args) {
  context = (typeof context === 'object' ? context : window)
  // 防止覆盖掉原有属性
  const key = Symbol()
  // 这里的this为需要执行的方法
  context[key] = this
  // 方法执行
  const result = context[key](...args)
  delete context[key]
  return result
}
代码语言:text
复制
// 验证样例
function fun(arg1, arg2) {
  console.log(this.name)
  console.log(arg1 + arg2)
}
const _this = { name: 'YIYING' }
// 参数为数组;方法立即执行
fun.ownApply(_this, [1, 2])
代码语言:text
复制
// 输出:
YIYING
3

参考:前端手写面试题详细解答

bind

定义与使用

Function.prototype.bind() : developer.mozilla.org/zh-CN/docs/…

代码语言:text
复制
// Function.prototype.bind()样例
function fun(arg1, arg2) {
  console.log(this.name)
  console.log(arg1 + arg2)
}
const _this = { name: 'YIYING' }
// 只变更fun中的this指向,返回新function对象
const newFun = fun.bind(_this)
newFun(1, 2)
代码语言:text
复制
// 输出:
YIYING
3
手写实现
代码语言:javascript
复制
/** * 自定义bind实现 * @param context     上下文 * @returns {Function} */
Function.prototype.ownBind = function(context) {
  context = (typeof context === 'object' ? context : window)
  return (...args)=>{
    this.call(context, ...args)
  }
}
代码语言:text
复制
// 验证样例
function fun(arg1, arg2) {
  console.log(this.name)
  console.log(arg1 + arg2)
}
const _this = { name: 'YIYING' }
// 只变更fun中的this指向,返回新function对象
const newFun = fun.ownBind(_this)
newFun(1, 2)
代码语言:text
复制
// 输出:
YIYING
3

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • call
    • 定义与使用
      • 手写实现
      • apply
        • 定义与使用
          • 手写实现
          • bind
            • 定义与使用
              • 手写实现
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档