a.js
let str="hello";
let update=(s)=>{
    str=s;
}
module.exports={
    str,
    update,
}
b.js
const{str,update}=require("./a");
console.log(str);
update("world");
console.log(str);
有什么方法可以动态修改吗
|      1wukongkong      2020-12-06 10:29:18 +08:00 现在不能吗?好像 important 形式是可以的,一个是传递值,一个是传递引用 | 
|      2mxT52CRuqR6o5      2020-12-06 11:29:58 +08:00 module.exports = { get str(){ return str}, update, } | 
|      3mxT52CRuqR6o5      2020-12-06 11:31:19 +08:00 | 
|  |      4yazoox      2020-12-06 13:11:51 +08:00 添加一个方法,getstr(),返回 str 的值。然后,export getstr 方法就可以了。 | 
|      5morethansean      2020-12-06 18:12:06 +08:00  1 用 ES module 或者 exports.str = 123; exports.update = s => exports.str = s; 因为 JS 里通常都是值传递,除了个别情况比如 object(包括数组) 或者 arguments,你赋值给 module.exports 的对象里的 str 属性是一个值不是一个引用。 | 
|      6SergeGao      2020-12-06 22:54:21 +08:00 楼上+1, 用 es module 或者用 get |