first.js

var a='this is first.js'

module.exports=a;


second.js

var a=require('./first');

console.log(a);



  输出:这是first.js


如果我更改second.js中“ a”的内容,那也会在first.js中反映出来吗?如果没有,如果可能的话该怎么做?

first.js

var a='this is first.js'

module.export=a;


second.js

var a=require('./first');

console.log(a);

最佳答案

您需要传递一个对象而不是字符串。

first.js

var a = {

    txt : 'this is first.js'
}

function too() {
    console.log(a.txt);
}
module.exports = { foo: a, too:too };


在app.js中,您可以对其进行修改,并将其反映在其他地方

var a = require("./first");
a.foo.txt = 'hahaha';
console.log(a.foo.txt);
a.too();


希望对您有所帮助。

10-04 15:33
查看更多