2016-07-28 6 views

答えて

1

exportsは、何かをする前にmodule.exportsに対応するオブジェクトです。私はそれはいくつかのレガシーコードのためだと思うが、基本的に人々はを使用してオブジェクト全体を独自のオブジェクトまたは関数に置き換えたい場合、exportsを使用し、モジュールから機能を停止させたい場合に使用します。そうでない私の知る限り、

const mod = require('that-module'); 
mod.install(params, callback); // call that function 

あなたが見ているフレームワークは、おそらくブートストラッププロセスの一部として、それを使用している:それは最初は少し混乱だが、基本的にexports.installは、単にコードを呼び出すことのような何かをすることを意味しますノードエンジン自体にとって重要です。

0

はい、同じことです。コードを設定する2つの方法のいずれかを使用できます。

別のものはmemoryです。同じメモリを指しています。あなたは変数のようexportsを考えることができ、あなたのモジュールをエクスポートするには、この方法を使用することはできません。

このモジュールを考える:

// test.js 
exports = { 
    // you can not use this way to export module. 
    // because at this time, `exports` points to another memory region 
    // and it did not lie on same memory with `module.exports` 
    sayHello: function() { 
     console.log("Hello !"); 
    } 
} 

次のコードは、エラーを取得します:TypeError: test.sayHello is not a function

// app.js 
var test = require("./test"); 
test.sayHello(); 
// You will get TypeError: test.sayHello is not a function 

module.exportsを使用してモジュールをエクスポートする正しい方法:

// test.js 
module.exports = { 
    // you can not use this way to export module. 
    sayHello: function() { 
     console.log("Hello !"); 
    } 
} 

// app.js 
var test = require("./test"); 
test.sayHello(); 
// Console prints: Hello ! 

これは単に開発者のスタイルです。

関連する問題