是否可以让其返回输出?
hello.js
var y = 1;
console.log(x);
console.log(y);
main.js
var x = 42;
var magic = somehowInclude('hello.js');
当您使用 Node 运行 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);
阅读 module loading system 的详细说明在 Node.js 文档中。
关于javascript - Nodejs加载并执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099521/