2016-07-06 108 views
0

所以我拉了两种不同的状态。然后对于每个状态我创建了一个函数,每个都打印一个不同的控制台消息。现在我可以做的是,在我实例化我的课后,我可以调用其中一种方法或两种方法手动打印控制台消息。但是,我怎样才能打印基于状态为'ready'或'saveSuccess'的控制台消息?我的代码如下。如何根据状态显示Console.log?

'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

会像这样不行?

'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; 
+0

这应该是实际工作 –

+0

你介意接受答案,如果它适合你吗?谢谢! –

+0

对不起Héctor!这是被接受的。谢谢 –