Nodejs加载并执行文件
有可能有这个返回一个输出?
hello.js
var y = 1; console.log(x); console.log(y);
main.js
var x = 42; var magic = somehowInclude('hello.js');
而当你用节点运行main.js
,它打印出42
和1
是否可以做到这一点,没有require
和exports
?
使用Node.js模块。
hello.js
module.exports.magic = function (x) { var y = 1; console.log(x); console.log(y); };
main.js
var x = 42; var hello = require('./hello.js'); hello.magic(42);
阅读Node.js文档中的模块加载系统的详细描述。