很多时候可以看到,在node中有两种方法可以输出变量:
方法一:对module.exports赋值:
function hello(){
console.log('hello word');
}
function greet(name){
console.log('hello'+name);
}
module.exports = {
hello:hello,
greet:greet
}
方法二直接使用exports:
function hello(){
console.log('hello word');
}
function greet(name){
console.log('hello'+name);
}
function hello(){
console.log('hello word');
}
exports.hello = hello;exports.greet = greet;
//但是不可以直接对exports赋值,直接赋值的话代码可以执行,但是没有输出任何变量(exports = {hello = hello,greet = greet})。
load()函数最终返回module.exports;
var load = function(exports,module){
return module.exports;
}
var exported = load(module.exports,module);
也就是说,默认情况下node准备的exports和module.exports实际上指向同一个空对象,但是如果我们输出的是函数或者数组,就只能给module.exports赋值。