在方法中更新此变量

时间:2019-04-30 01:42:12

标签: javascript

可能是愚蠢的问题。我环顾四周找不到很多东西。本质上,我试图在对象上的方法中仅更新对象上的此变量。

就像反应一样,您可以执行以下操作:setState({foo:bar});那么该变量在整个对象中都可用。

我想要什么

class test {
    constructor() {

    }
    update() {
        this.foo = bar;
    }
    log() {
        console.log(this.foo);
    }
}

const poptarts = new test();

poptarts.update();

poptarts.log();

编辑


const axios = require('axios');

class test{
    constructor(token) {
        this.token = token

        this.getProfile = this.getProfile.bind(this);
        this.logProfile = this.logProfile.bind(this);
    }
    async getProfile() {
        const response = await axios.get(`api`);
        console.log(response);
        this.profile = response.data;
        this.test = 'test'
    }
    logProfile() {
        console.log(this.profile);
    }
};

module.exports = test;

0 个答案:

没有答案