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