如果我有此hello.js文件:
var greeting = function() {
console.log('Hello');
}
module.exports = greeting;
然后在main.js中:
var temp = require('./hello.js');
temp();
当您说的module.exports = greeting
是将greeting函数附加到模块上的exports对象时。因为当我需要时hello.js
,main.js
我能够直接调用temp()。并且不必像temp.greeting()那样做;
这是否意味着由于require
returns module.exports
它只是返回导出对象上的方法而不是完全返回导出对象,所以正确吗?我很困惑为什么它返回的是导出对象(问候函数)上的内容,而不是真正的导出对象本身。
require(...)
module.exports
从该模块返回。这通常是一个对象,但也可以是其他任何东西(通常是一个函数),例如在您的模块仅导出单个函数的情况下。
这样做没有错-module.exports
只是一个简单的对象(module.exports = {};
代码中最有可能在模块js文件内容“周围”运行的地方)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句