2016-07-06 5 views
2

次のエラーが発生しています。未定義のエラーのプロパティを取得するのはなぜですか?

Uncaught TypeError: Cannot read property 'on' of undefined 

私はコード

let ServicePlugin = new ServicePlugin(); 

の行に私が間違っているの何任意のアイデアを追加したときにそれが起こりますか?以下は完全なコードです。

'use strict'; 

class ServicePlugin { 
    constructor(api) { 
    this._api = api; 

    this._api.on('ready',() => this._apiReady()); 
    this._api.on('saveSuccess',() => this._apiSuccess()); 
    } 

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

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

} 

let ServicePlugin = new ServicePlugin(); 
ServicePlugin._apiReady(); 

module.exports = ServicePlugin; 
+7

'new AnnServicePlugin();'あなたはコンストラクタに何を渡しましたか? - '未定義です。 'だから、コンソールはあなたに横たわっていません。単なる 'this._learnosity = undefined;と' on() 'は' undefined'に定義されていません。 – Tushar

+0

「学習」とは何ですか?それはどこに定義されていますか? –

+2

問題は、コンストラクタが引数なしで呼び出されることです。その結果、「学習」は未定義である。 –

答えて

3

あなたのクラスをインスタンス化するときは、(それが何であれ)learnosityパラメータを提供する必要があります。 AnnServicePluginはそれを期待:もちろん

let annServicePlugin = new AnnServicePlugin(learnosity); 

learnosityオブジェクトが期待onメソッドを持っている必要があります。

関連する問題