【发布时间】:2015-12-26 04:18:27
【问题描述】:
是否可以使用节点模块来实现此行为?
module.js:
module.exports = function () {
var test.fn = function () {
console.log("$test.fn()");
}
var test.fn2 = function () {
console.log("$test.fn2()");
}
var variable = "test";
};
app.js:
require("./module.js")();
test.fn();
test.fn2();
otherFunction(variable);
我不想做这样的事情$ = require("./module.js")(); $.test.fn();
我想在没有包装变量的情况下将此变量注入 app.js 范围。
编辑:
我最终使用了这个:
module.js:
module.exports = function () {
eval(String(inject));
var inject = (
this.$module1 = {},
this.$module1.fn = function () {
console.log("$module1.fn()");
}
);
};
app.js:
require("./module.js")();
$module1.fn();
【问题讨论】:
标签: javascript node.js scope node-modules