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

浙公网安备 33010602011771号