我在Node.js模块中找到了以下契约:
module.exports = exports = nano = function database_module(cfg) {...}
我想知道module.exports和exports之间有什么区别,为什么在这里使用它们。
我在Node.js模块中找到了以下契约:
module.exports = exports = nano = function database_module(cfg) {...}
我想知道module.exports和exports之间有什么区别,为什么在这里使用它们。
当前回答
JavaScript通过引用的副本传递对象
这与JavaScript中通过引用传递对象的方式有细微差别。
exports和module.exports都指向同一个对象。exports是变量,module.exports是模块对象的属性。
假设我写了这样的东西:
exports = {a:1};
module.exports = {b:12};
exports和module.exports现在指向不同的对象。修改导出不再修改module.exports。
当import函数检查module.exports时,它得到{b:12}
其他回答
我只是做了一些测试,结果发现,在nodejs的模块代码中,应该是这样的:
var module.exports = {};
var exports = module.exports;
so:
1:
exports = function(){}; // this will not work! as it make the exports to some other pointer
module.exports = function(){}; // it works! cause finally nodejs make the module.exports to export.
2:
exports.abc = function(){}; // works!
exports.efg = function(){}; // works!
3:但是,在这种情况下
module.exports = function(){}; // from now on we have to using module.exports to attach more stuff to exports.
module.exports.a = 'value a'; // works
exports.b = 'value b'; // the b will nerver be seen cause of the first line of code we have do it before (or later)
JavaScript通过引用的副本传递对象
这与JavaScript中通过引用传递对象的方式有细微差别。
exports和module.exports都指向同一个对象。exports是变量,module.exports是模块对象的属性。
假设我写了这样的东西:
exports = {a:1};
module.exports = {b:12};
exports和module.exports现在指向不同的对象。修改导出不再修改module.exports。
当import函数检查module.exports时,它得到{b:12}
module.export和exports都指向同一个对象,然后对模块求值。
使用require语句在另一个模块中使用模块时,添加到module.exports对象的任何属性都将可用。导出是一种快捷方式,可用于相同的事情。例如:
module.exports.add = (a, b) => a+b
相当于写:
exports.add = (a, b) => a+b
因此,只要不为导出变量分配新值,就可以了。当你这样做时:
exports = (a, b) => a+b
当您为导出分配新值时,它不再引用导出的对象,因此将保持模块的本地状态。
如果您计划为module.exports分配一个新值,而不是向可用的初始对象添加新的财产,您可能应该考虑如下所示:
module.exports = exports = (a, b) => a+b
Node.js网站对此有很好的解释。
在节点js中,module.js文件用于运行module.load系统。每当节点执行文件时,它都会将js文件内容包装如下
'(function (exports, require, module, __filename, __dirname) {',+
//your js file content
'\n});'
由于这种包装在urjs源代码中,您可以访问导出、require、模块等。。之所以使用这种方法,是因为没有其他方法可以将js文件中的功能写入另一个文件。
然后节点使用c++执行这个包装函数。此时,传递到此函数的exports对象将被填充。
您可以在这个函数中看到参数导出和模块。实际上,exports是模块构造函数函数的公共成员。
查看以下代码
将此代码复制到b.js中
console.log("module is "+Object.prototype.toString.call(module));
console.log("object.keys "+Object.keys(module));
console.log(module.exports);
console.log(exports === module.exports);
console.log("exports is "+Object.prototype.toString.call(exports));
console.log('----------------------------------------------');
var foo = require('a.js');
console.log("object.keys of foo: "+Object.keys(foo));
console.log('name is '+ foo);
foo();
将此代码复制到.js
exports.name = 'hello';
module.exports.name = 'hi';
module.exports.age = 23;
module.exports = function(){console.log('function to module exports')};
//exports = function(){console.log('function to export');}
现在使用节点运行
这是输出
module is [object Object]
object.keys id,exports,parent,filename,loaded,children,paths
{}
true
导出为[对象对象]
foo的object.keys:名称是函数(){console.log('模块导出函数')}功能到模块导出
现在删除.js中的注释行,并注释该行上方的行然后删除b.js的最后一行并运行。
在javascript世界中,不能重新分配作为参数传递的对象,但当该函数的对象设置为另一个函数的参数时,可以更改该函数的公共成员
请记住
仅当您想在使用require关键字时获取函数时,才使用module.exports。在上面的例子中,我们varfoo=require(a.js);你可以看到我们可以将foo作为函数调用;
节点文档就是这样解释的导出对象是由Module系统创建的。有时这是不可接受的,许多人希望自己的模块是某个类的实例。为此,请将所需的导出对象分配给Module.exports
这是曼宁出版的动作书中关于node.js中节点模块的一个很好的描述。最终在应用程序中导出的是module.exports.exportsup简单地作为module.exports的全局引用,它最初定义为可以向其中添加财产的空对象。所以exports.myFunc只是一种速记对于module.exports.myFunc。因此,如果将导出设置为其他值,则会断开引用模块导出和导出。因为module.exports是真正的导出,导出将不再按预期工作,它不引用模块.不再导出。如果要维护该链接,可以创建module.exports再次引用导出如下:
module.exports = exports = db;