如何根据状态显示Console.log?

时间:2016-07-06 08:52:54

标签: javascript ecmascript-6

所以我已经进入了两个不同的状态。然后为每个状态创建一个函数,每个函数都打印一个不同的控制台消息。在那一刻,我可以做的是,在我实例化我的类后,我可以调用任一方法或两者同时手动打印它们的控制台消息。但是,如何基于“准备好”或“saveSuccess”状态打印控制台消息?我的代码如下。

'use strict';

class AnnServicePlugin {
  constructor(learnosity) {
    this._learnosity = learnosity;

    this._learnosity.on('ready', () => this._learnosityReady());
    this._learnosity.on('saveSuccess', () => this._learnositySuccess());
  }

  _learnosityReady() {
    console.log('Plugin Ready')
  }

  _learnositySuccess() {
    console.log('Plugin Success');
  } 
}

let annServicePlugin = new AnnServicePlugin(learnosity);
annServicePlugin._learnosityReady();

module.exports = AnnServicePlugin;

先谢谢:)

1 个答案:

答案 0 :(得分:1)

这样的事情不起作用吗?

'use strict';

class AnnServicePlugin {
  constructor(learnosity) {
    this._learnosity = learnosity;

    this._learnosity.on('ready', () => this._learnosityReady());
    this._learnosity.on('saveSuccess', () => this._learnositySuccess());
  }

  _learnosityReady() {
    this._state = 'ready'
  }

  _learnositySuccess() {
    this._state = 'success'
  } 

  _learnosityPrint() {
    if (this._state == 'ready') {
       console.log('Plugin Ready')
    } else if (this._state == 'success') {
       console.log('Plugin Success')
    }
  }
}

let annServicePlugin = new AnnServicePlugin(learnosity);
annServicePlugin._learnosityPrint();

    module.exports = AnnServicePlugin;