2016-08-17 3 views
1

nodejsがファイル内の関数のコンテキストとは何ですか?私は、輸出の機能が必要な場合 私はちょうどnodejsによるファイル内の関数のコンテキストは何ですか?

module.exports = function() {}; 
or 
module.someFunction = function() {}; 

を書くしかし、どのような文脈では、モジュールなしで機能を持っていますか?

function someFunction() {}; 
console.log(module); // not someFunction 
console.log(module.exports); // not someFunction 

P.S.このファイルで宣言された関数のリストはどこで見ることができますか?ブラウザで私はwindowを持っています。 nodejsにはglobalmodulemodule.exportsがありますが、そのうちの1つに宣言された機能がありません。

+0

ブラウザに 'window'のような' context'はありません! – Rayon

+0

あなたは何を意味しているのですか? – Rayon

答えて

1

しかし、どのコンテキストがモジュールなしで機能していますか?

通常のJavaScript機能と同じです。厳密モードでは、コンテキストはundefined

(function() { 
    'use strict'; 
    console.log(this === undefined); 
})(); 
// true 

とずさんなモード(非厳密モード)グローバルオブジェクトであろう。

(function() { 
    console.log(this === global); 
})(); 
// true 

注:あなたはthisが参照するかと思っている場合は、モジュールレベルでは、それはexportsオブジェクトになります。詳しい説明はthis answerにあります。

+0

「それを回す」とはどういう意味ですか? – thefourtheye

関連する問題