👨‍💻
Hong's 前端笔记
  • 🌈About
  • JavaScript
    • 作用域
    • 闭包
    • this
      • bind()
    • 原型
      • Object.create()
      • 模拟“类”
      • ES6 Class
      • Function
    • 对象
    • 类型 & 值
      • 原生函数
      • 数组
      • 字符串
    • 异步
    • JavaScript 相关概念
      • Event Loop 事件循环
      • Prototype 原型
      • Context 执行上下文
      • this
      • Promise
    • JavaScript 常见问题
      • 手写实现
      • 看代码输出
  • React
    • 核心概念
    • 深入理解
      • State
      • 如何管理 State
      • React 渲染过程
      • 处理 DOM 事件
      • Pure 组件
      • Context
      • Key 属性
      • React.lazy()
      • Render Props
    • Hooks
      • useState
      • useRef
      • useEffect
        • Some details
      • 相关概念
      • 自定义 Hook
      • useEffect vs useLayoutEffect
    • React 相关问题
      • Class vs Function
  • React Libraries
    • Redux
      • Quick Start
      • 如何持久化 Redux 数据
      • 异步操作
      • 性能优化
      • Dive into immutability
    • Route v6
      • Quick Start
  • Reactivity
    • Immer
  • Vue
    • 深入理解
      • Composition vs Options
      • Reactivity
  • Network
    • Cookies
    • HTTP
    • HTTPS
    • CORS 跨域资源共享
    • 认证手段
    • 安全相关
    • 网络相关知识
  • Browser
    • DOM 操作
    • Events 事件
    • XHR & Fetch
    • 性能优化
    • HTML 相关概念
    • 浏览器相关概念
  • CSS
    • 盒模型
    • Layout 布局
    • Styles 样式
    • CSS 相关概念
    • CSS 相关技巧
      • 移动端适配
      • Flex
      • 动画
  • TypeScript
    • Quick Peek
    • Types in TypeScript
    • Narrowing
    • Functions in TypeScript
  • Workflow
    • Webpack
      • Webpack 概念
      • 资源管理
      • 管理输出
      • 开发环境配置
      • 生产环境配置
      • 优化代码运行性能
  • Others
    • 小程序与原生 Web 的区别
  • SSO
    • About
    • API Doc
    • 接入指南
Powered by GitBook
On this page
  • call apply bind
  • this 指向
Edit on GitHub
  1. JavaScript
  2. JavaScript 相关概念

this

call apply bind

function Base(foo) {
    this.foo = foo
}
function Child(foo) {
    Base.call(this, foo)	// 可以理解成继承,不过是把 this 替换成了 Child 的了
}
const child = new Child('foo')

相同

改变函数内部的 this 指向

区别

call,apply 会调用函数;而 bind 不会调用

call 接受若干参数,apply 接受一整个数组

应用

  • call 多用作继承

  • apply 多用于跟数组有关的操作,如求 max, min

    const arr = [1, 22, -10, 0, 9]
    const max = Math.max.apply(Math, arr)
    console.log(max)		// 22
    // 利用 ... 运算符也可以
    const max = Math.max(...arr)
  • bind 不调用函数,但会改变内部 this

    x = 0       // global
    const foo = {
        x: 10,  // foo
        getX: function() { return this.x }
    }
    
    console.log(foo.getX())     // 10
    
    const retrieveX = foo.getX
    console.log(retrieveX())    // 0,函数里的 this 实际指向的是 global
    
    // 注意 bind 返回的才是绑定过的对象
    const boundGetX = retrieveX.bind(foo)
    console.log(boundGetX())    // 10, 函数里的 this 重新指向了 foo

this 指向

普通函数中的 this

谁调用了这个函数,this就指向谁

function foo() {
    console.log(this)
}
const obj = {
    bar: function() {
        console.log(this)
    }
}
foo()       // window,因为 foo 是在全局作用于调用的
obj.bar()   // obj

匿名函数的 this

匿名函数的 this 具有**全局性**,指向 window

const obj = {
    bar: function () {
        return function () { console.log(this) }
    }
}
obj.bar()()   // window

箭头函数的 this

  • 箭头函数的 this 是在定义时候确定下来的,而不是在调用时决定的

  • this 指向父级作用域的上下文

    如何寻找箭头函数的 this 指向?

    找到离箭头函数最近的function,与该function平级的执行上下文中的this即是箭头函数中的this

  • 箭头函数无法使用 apply,call,bind 改变 this 指向

const obj = {
    bar: function () {
        return () => console.log(this)
    }
}
obj.bar()()   // obj

Last updated 3 years ago