JavaScript函数中call、apply和bind的详解和手动实现

name:“李逍遥”

}

obj.sum.call(obj1, 1, 2) // 李逍遥 3

/* 正常模式 */

obj.sum.call() // window

obj.sum.call(undefined, 1, 2) // window

obj.sum.call(null, 1, 2) // window

/* 严格模式 */

‘use strict’

obj.sum.call() // undefined

obj.sum.call(undefined, 1, 2) // undefined

obj.sum.call(null, 1, 2) // null

手动模拟实现

设计逻辑:

  • myCall() 方法被添加在 Function 原型对象上,目标函数调用该方法时,myCall() 方法内部的 this 将指向目标函数。

  • 将目标函数作为 context 对象的方法来执行,由此目标函数内部的 this 将指向 context 对象。

  • 从 context 对象中删除目标函数

  • 使用扩展运算符 … 处理传入目标函数的参数

Function.prototype.myCall = function (context, …args) {

if (context === undefined || context === null) {

context = window

}

context.fn = this

const result = context.fn(…args)

delete context.fn

return result

}

let obj1 = {

num: 1,

sum(a, b) {

console.log(this)

return this.num + a + b

}

}

let obj2 = {

num: 10

}

console.log(obj1.sum.call(obj2, 2, 3)) // 15

console.log(obj1.sum.myCall(obj2, 2, 3)) // 15

apply()和手动模拟实现

=============================================================================

apply()方法

调用 apply() 方法会立即执行目标函数,同时改变函数内部 this 的指向。this 指向由方法的第一个参数决定,第二个参数是一个参数数组或 arguments 对象,各数组元素或 arguments 对象表示的各参数将作为目标函数的参数一一对应传入。

obj1.sum.apply(obj2, [2, 3])

手动模拟实现

设计逻辑:

  • myApply() 方法被添加在 Function 原型对象上,目标函数调用该方法时,myApply() 方法内部的 this 将指向目标函数。

  • 将目标函数作为 context 对象的方法来执行,由此目标函数内部的 this 将指向 context 对象。

  • 从 context 对象中删除目标函数

  • 使用扩展运算符 … 处理传入目标函数的参数

Function.prototype.myApply = function (context, args) {

if (context === undefined || context === null) {

context = window

}

// 下面这行为核心代码

context.fn = this

const result = context.fn(…args)

delete context.fn

return result

}

console.log(obj1.sum.apply(obj2, [2, 3])) // 15

console.log(obj1.sum.myApply(obj2, [2, 3])) // 15

bind()和手动模拟实现

============================================================================

bind()方法

调用 bind() 方法也会改变函数内部 this 的指向,但是它不是调用函数,而是返回一个新函数——目标函数的拷贝,该函数内部的 this 指向方法的第一个参数,后面逐个列举的任意个参数将作为目标函数的参数一一对应传入。之后执行新函数相当于执行了目标函数。

bind() 方法实现了函数柯里化,因此可以分两次向目标函数传递参数,第一次的参数列举在 bind() 方法首参后面,第二次的参数列举在新函数中。

obj1.sum.bind(obj2, 2)(3)

手动模拟实现

设计逻辑:

  • myBind() 方法被添加在 Function 原型对象上,目标函数调用该方法时,myBind() 方法内部的 this 将指向目标函数。

  • 将目标函数作为 context 对象的方法来执行,由此目标函数内部的 this 将指向 context 对象。

  • 从 context 对象中删除目标函数

  • 使用扩展运算符 … 处理传入目标函数的初始参数、后续参数。

Function.prototype.myBind = function (context, …initArgs) {

if (context === undefined || context === null) {

context = window

}

// 缓存 this 值

const _this = this

return function (…args) {

// 下面这行为核心代码

context.fn = _this

const result = context.fn(…initArgs, …args)

delete context.fn

return result

}

}

Vue

  • 什么是MVVM?

  • mvvm和mvc区别?它和其它框架(jquery)的区别是什么?哪些场景适合?

  • 组件之间的传值?

  • Vue 双向绑定原理

  • 描述下 vue 从初始化页面–修改数据–刷新页面 UI 的过程?

  • 虚拟 DOM 实现原理

  • Vue 中 key 值的作用?

  • Vue 的生命周期

  • Vue 组件间通信有哪些方式?

  • vue 中怎么重置 data?

  • 组件中写 name 选项有什么作用?

  • Vue 的 nextTick 的原理是什么?

  • Vuex 有哪几种属性?

  • 12
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值