2016-04-26 8 views
1

このメテオコードはsend関数を呼び出そうとしますが、サーバーは「送信が定義されていません」というエラーを報告します。送信します。パブリックメソッドからモジュールのプライベート関数を呼び出す

なぜ、どのように修正するのですか?おかげ

request = (function() { 
    const paths = {logout: {method: 'GET'}} 
    const send =() => {some code} 

    return { 
    registerRequestAction: (path, func) => { 
     paths[path].action = func; 
    }, 
    invoke: (type) => { 
    paths[type].action(); 
    }  
    } 

    }()); 

request.registerRequestAction('logout',() => { 
send(); // send is not defined 
request.send(); // object has no method send 

}); 

request.invoke('logout'); // to fire it up 

答えて

1

あなたはsendメソッドを参照せずに匿名オブジェクトを返すされています

return { 
    registerRequestAction: (path, func) => { 
      paths[path].action = func; 
    }, 
    invoke: (type) => { 
     paths[type].action(); 
    }, 
    // expose send to the outside 
    send: send 
} 

request.registerRequestAction('logout',() => { 
    request.send(); 
}); 

// this isn't visible from the outside 
    const send =() => {some code} 

    // this is visible from the outside, 
    // but with no reference to send() 
    return { 
    registerRequestAction: (path, func) => { 
     paths[path].action = func; 
    }, 
    invoke: (type) => { 
    paths[type].action(); 
    }  
    } 

はあなたの問題を解決する必要があり、このような何かをやって

関連する問題