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


类中,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() //输出[]
    }, 
})

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM