this 的相关知识,内容包括函数中的 this、类中的 this、博客参考。

一、函数中的 this

  • this 是什么?一般是实例对象
  • 怎么用?结合语境灵活运用

1. 普通函数与 this

  1. function f(){
  2. return this
  3. }
  4. let object = {
  5. n:0
  6. }
  7. console.dir(f.call()) // window ———— Window 的实例
  8. console.dir(f.call(object)) // object ———— Object 的实例

记住这两条!!!

  • 函数只是函数,具体 this 是什么,要看传给它的 this 是什么
  • 当传递的 this 为空时,this 默认指向 window

2. 箭头函数与 this

  • 箭头函数里是没有 this 的,因为它不接受 this 参数,它油盐不进
  1. let object2 = {
  2. n:0,
  3. f2:()=>{
  4. return this
  5. },
  6. f3(){
  7. return this
  8. }
  9. }
  10. console.log(object2.f2()) // window ———— Window 的实例
  11. console.log(object2.f3()) // object2 ———— Object 的实例

分析:

  • 在最外部,this 的指向为 window
  • object2.f2() 时,object2 传给它新 this,f2() 油盐不进,因此 this 还是指向 window
  • object2.f3() 时,object2 传给它新 this,f2() 接受了新 this,此时 this 指向 object2

3. 算清楚 this

this 是跟着函数的“语境”变化的,想要清楚 this 的指向一步一步推,用完整写法 .bind(),把函数传参搞清楚,这样一定能搞定。

4. 箭头函数与 this 配合

  1. ()=>{f()}
  2. f

方式一和方式二直接或间接调用函数 f,当满足以下条件时,使用方式一更好

  • 这个函数是给外部调用的
  • f 函数里面用到了 this

原因是当外部函数调用函数时会传入一个 this,如果 f 函数里面有 this,会把里面的旧 this 换成 新 this

  • 普通函数会接受 this 参数,改变 this 指向
  • 箭头函数不接受 this 参数,外部函数传给它的 this,它假装没看见

二、类中的 this

1. 定义一个类

  • 定义类的时候,里面必须要写一个「构造函数」—— constructor
  • 构造函数中的 this 指向类的「实例」
  • 我们在构造函数里把属性挂到创建的「实例」上面
  1. class Reactangle{
  2. constructor(length,width){
  3. this.length = length
  4. this.width = width
  5. this.name = "reactangle"
  6. }
  7. area(){
  8. return this.length*this.width
  9. }
  10. perimeter(){
  11. return (this.length + this.width)*2
  12. }
  13. }
  14. let reactangle = new Reactangle(3,4)
  15. console.dir(reactangle)

image.png

  • new 命令的作用是执行构造函数,并返回一个实例对象
  • 在构造函数外部的函数就是类的「方法」,它没有挂在实例上

2. 类中的函数

把「方法」挂在实例上也是可以的,把函数写在「构造函数」里

  1. class Reactangle{
  2. constructor(length,width){
  3. this.length = length
  4. this.width = width
  5. this.name = "reactangle"
  6. this.area = ()=>{
  7. return this.length*this.width
  8. }
  9. this.perimeter = ()=>{
  10. return (this.length + this.width)*2
  11. }
  12. }
  13. }
  14. let reactangle = new Reactangle(3,4)
  15. console.dir(reactangle)

image.png

3. 另一种函数写法

把函数写到构造函数外部,但是能够把它挂到「实例」上

  1. class Reactangle{
  2. constructor(length,width){
  3. this.length = length
  4. this.width = width
  5. this.name = "reactangle"
  6. }
  7. area = ()=>{
  8. return this.length*this.width
  9. }
  10. perimeter = ()=>{
  11. return (this.length + this.width)*2
  12. }
  13. }
  14. let reactangle = new Reactangle(3,4)
  15. console.dir(reactangle)

三、 博客参考

《JavaScript 的 this 原理》—— 阮一峰:点击这里
《this 的值到底是什么》—— 方应杭:点击这里

「@浪里淘沙的小法师」_