我的网站开发技术经验总结 我的网站开发技术经验总结
首页

fangdown

我的网站开发技术经验总结
首页
  • 大前端

  • 基础

    • js

      • 你知道new操作符是如何实现的吗
      • 说说大文件上传的思路
      • 说说对闭包的理解及应用
      • 说说对单点登录sso的理解
      • 说说对防抖节流的理解
      • 说说对函数式编程的理解
      • 说说对内存泄漏的理解及触发场景
      • 说说对事件模型的理解
      • 说说对事件委托的理解及应用场景
      • 说说对原型及原型链的理解
      • 说说对正则表达式的理解
      • 说说对BOM的理解及常见操作
      • 说说对Dom的理解及常见操作
      • 说说对JavaScript中事件循环的理解
      • 说说对js的执行上下文的理解
      • 说说对js中变量作用域的理解
      • 说说对js中继承的理解及实现方式
      • 说说对this关键字的理解
      • 说说你对 Immutable Data的理解?如何应用在React项目中
      • 说说如何判断数据类型
      • 说说如何判断一个元素在可视区域内
      • 说说如何实现函数缓存
      • 说说深浅拷贝的区别及实现
      • 说说什么是尾递归及其应用
      • 说说为什么0.1+0.2!==0.3
      • 说说下拉刷新,上拉加载的原理
      • 说说ajax的实现原理
      • 说说call-apply-bind的作用及区别
        • 是什么
        • 怎么用
          • call
          • apply
          • bind
        • 原理
      • 说说js中本地存储有哪些方式及区别
      • 说说js中的类型转换机制
      • Javscript数组的常用方法有哪些?
      • Javscript字符串的常用方法有哪些?
    • css

    • ES6

  • 框架

  • 情商

  • 算法

  • 网络

  • 千锤百炼
  • 基础
  • js
fangdown
2021-08-31
目录

说说call-apply-bind的作用及区别

# 是什么

作用:改变函数执行的上下文, 简单的说就是更改函数执行的this指向

# 怎么用

# call

第一个参数为null, undefined时, this指向window

let arr = [1,2,3,4]
Math.max.call({}, ...arr)

# apply

let arr = [1,2,3,4]
Math.max.apply({}, arr)

# bind

function fn(...args){
  console.log(this, args)
}
let obj = {
  name: 'fang'
}
let bindFn = fn.bind(obj)

bindFn(1,2) // obj, 1, 2
fn(1,2) // window ,1, 2

# 原理

  • call
Function.prototype.myCall = function(context){
  context.fn = this
  let args = [...arguments].slice(1)
  let result = context.fn(...args)
  delete context.fn
  return result
}
let arr = [1,2,3,4]
Math.max.myCall({}, ...arr)
  • apply
Function.prototype.myApply = function(context,arr){
  context.fn = this
  let args = arr
  let result = context.fn(...args)
  delete context.fn
  return result
}
let arr = [1,2,3,4]
Math.max.myApply({}, arr)
  • bind
Function.prototype.myBind = function(context){
  let fn = this
  let args = [...arguments].slice(1)
  // 返回一个函数
  return function Fn(){
    // 根据类型不同,返回不同
    // 有没有想到eventbus中的once
    return fn.apply(this instanceof Fn? 
    new fn(...arguments) : 
    context, args.concat(...arguments)
    )
  }
  let result = context.fn(...args)
  delete context.fn
  return result
}
#js
上次更新: 2021/12/19, 18:05:42
说说ajax的实现原理
说说js中本地存储有哪些方式及区别

← 说说ajax的实现原理 说说js中本地存储有哪些方式及区别→

最近更新
01
多分支修复撞车的问题
05-01
02
如何成为架构师
01-23
03
服务器部署全过程
11-23
更多文章>
Theme by Vdoing | Copyright © 2019-2026 fangdown | 粤ICP备19079809号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式