類中,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