1

是否有可能让它返回一个输出?

你好.js

var y = 1;

console.log(x);
console.log(y);

main.js

var x = 42;

var magic = somehowInclude('hello.js');

当您main.js使用节点运行时,它会打印421

require没有and可以做到这一点exports吗?

4

1 回答 1

1

使用 Node.js 模块。

你好.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 文档中关于模块加载系统的详细描述。

于 2012-04-11T09:37:15.610 回答