2016-07-06 67 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!這是被接受的。謝謝 –