类中,static方法中的this指向类本身

theme.js

class Theme {
  themes = [] //这个themes属于对象
  static async getThemes(){
    const themes = [1,2,3]
    this.themes = themes //this指向类,this.themes不是类中第一行定义的themes,等于在类中又新定义一个静态属性(static themes = [1,2,3])
    console.log(Theme.themes) //[1,2,3]
    console.log(this.themes) //[1,2,3]
  }
  async getHomeLocationA(){
    console.log(this.themes)  //this指向对象,也就是类中第一行定义的themes, 输出[]
  }
}
export {
  Theme
}

 

home.js

import {Theme} from '../../model/theme.js'

page({
    onLoad: function (options) {
       this.initAllData()
    },

    async initAllData(){
    const theme = new Theme()
    Theme.getThemes() //其实只是在Themes类中新定义了一个静态属性(static themes = [1,2,3]),并没有改变类中第一行定义的themes = []的值
    theme.getHomeLocationA() //输出[]
    }, 
})

 

相关文章:

  • 2022-12-23
  • 2021-09-28
  • 2021-07-21
  • 2019-01-11
  • 2022-12-23
  • 2021-07-09
  • 2021-10-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2020-11-09
  • 2022-12-23
相关资源
相似解决方案