2016-05-21 5 views
1

私は所与の文字列に「hello」を付加しますNode.jsのテスト・アプリケーションを作成しようとしています。Node.jsのコード/ファイル構造

私は、メインファイルとテストファイルがあります:私はこの間違っについてつもりかどうかはわかりませんが、それは前付加を定義間違っ感じた

var helloPrepender = (function() { 
    // your code goes here 
    function prepend(text){ 
     return "hello" + text; 
    } 
}()); 

// make prepender available via "require" in Node.js 
if (module.exports) { 
    module.exports = helloPrepender; 
} 

helloPrepend.jsを「VaRのhelloPrepender」

しかし、また、それはhelloPrependTesterのように思えるの内部関数の内部機能が正しくhelloPrependの機能にアクセスしていません。

答えて

0

ない答えが、二つの簡単なメモ、

var helloPrepender = (function() { 
    // your code goes here 
    function prepend(text){ 
     return "hello" + text; 
    } 
}()); 

// make prepender available via "require" in Node.js 
if (module.exports) { 
    module.exports = helloPrepender 
} 

helloPrepender

は、関数を返していません。

あなたが書くべき

var helloPrepender = (function() { 
    // your code goes here 
    return function prepend(text){ 
     return "hello" + text; 
    } 
}()); 

// make prepender available via "require" in Node.js 
if (module.exports) { 
    module.exports = helloPrepender 
} 

は、その後、一般的に言えば、これはそれをすべて解決し

function prepend(text){ 
    return "hello" + text; 
} 
module.exports = prepend 

あるいは、

module.exports = function (text){ 
    return "hello" + text; 
} 
+0

に単純化することができます。ありがとうございました –

関連する問題