在 Node.js 中的其他模块中通过引用使用数组绝对是这种情况吗?
Is it absolutely the case that arrays are used by reference in other modules in Node.js?
我有
// file cars.js
var bodyshop = require('./bodyshop')
var connections = [];
many functions which operate on connections. adding them, changing them etc.
code in this file includes things like
bodyshop.meld(blah)
bodyshop.mix(blah)
exports.connections = connections
然后
// file bodyshop.js
let cars = require('./cars');
even more functions which operate on connections. adding them, changing them etc.
code in this file includes things like
cars.connections[3].color = pink
cars.connections.splice(deleteMe, 1)
module.exports = { meld, mix, flatten }
bodyshop 中的代码如 cars.connections.splice(deleteMe, 1)
是否确实会从“the”连接中删除一个项目(即,唯一的连接,在 cars.js) 和 bodyshop 中的代码,例如 cars.connections[3].color = pink
确实会改变“the” self-same one and only connections 的索引 3 的颜色?
我在 bodyshop 末尾使用语法“module.exports = { }”而不是像“exports.meld 这样的三行,这完全可以/安全/可以接受吗? =融合”?
这句话真的完全正确吗?? "在Node.js中如果你从M导出一个数组,当在另一个需要M的模块X中使用该数组时,该数组将在X中通过引用,即不是通过复制" ... ?
我使用以下方法和您提到的数组创建了两个文件。
第一个文件:test1.js
const testArray = [];
const getArray = () => {
return testArray;
};
module.exports = {
testArray,
getArray
}
第二个文件:test2.js
const { testArray, getArray } = require('./test1');
console.log('testing the required array before modifying it');
console.log(getArray());
testArray.push('test');
console.log('testing the method result after modifying the required array content');
console.log(getArray());
如果您可以在本地创建上述文件并 运行 它们,您将看到以下结果。
>node test2.js
testing the required array before modifying it
[]
testing the method result after modifying the required array content
[ 'test' ]
观察到的点是,
- 是的,如果你想用语法
module.exports = { }
导出它没关系,这不是什么大问题。
- 如果任何方法在所需文件之外修改此数组,它也会影响此处,这是因为
require
将是引用,而不是副本。
一种可能的解决方案是创建它的 JSON 副本,同时 requiring
如下:
const { testArray, getArray } = require('./test1');
const testArrayCopy = JSON.parse(JSON.stringify(testArray));
console.log('testing the required array before modifying it');
console.log(getArray());
testArrayCopy.push('test');
console.log('testing the method result after modifying the required array content');
console.log(getArray());
这是结果:
>node test2.js
testing the required array before modifying it
[]
testing the method result after modifying the required array content
[]
注意:JSON复制不会帮助您正确解析 DateTime。
我有
// file cars.js
var bodyshop = require('./bodyshop')
var connections = [];
many functions which operate on connections. adding them, changing them etc.
code in this file includes things like
bodyshop.meld(blah)
bodyshop.mix(blah)
exports.connections = connections
然后
// file bodyshop.js
let cars = require('./cars');
even more functions which operate on connections. adding them, changing them etc.
code in this file includes things like
cars.connections[3].color = pink
cars.connections.splice(deleteMe, 1)
module.exports = { meld, mix, flatten }
bodyshop 中的代码如
cars.connections.splice(deleteMe, 1)
是否确实会从“the”连接中删除一个项目(即,唯一的连接,在 cars.js) 和 bodyshop 中的代码,例如cars.connections[3].color = pink
确实会改变“the” self-same one and only connections 的索引 3 的颜色?我在 bodyshop 末尾使用语法“module.exports = { }”而不是像“exports.meld 这样的三行,这完全可以/安全/可以接受吗? =融合”?
这句话真的完全正确吗?? "在Node.js中如果你从M导出一个数组,当在另一个需要M的模块X中使用该数组时,该数组将在X中通过引用,即不是通过复制" ... ?
我使用以下方法和您提到的数组创建了两个文件。
第一个文件:test1.js
const testArray = [];
const getArray = () => {
return testArray;
};
module.exports = {
testArray,
getArray
}
第二个文件:test2.js
const { testArray, getArray } = require('./test1');
console.log('testing the required array before modifying it');
console.log(getArray());
testArray.push('test');
console.log('testing the method result after modifying the required array content');
console.log(getArray());
如果您可以在本地创建上述文件并 运行 它们,您将看到以下结果。
>node test2.js
testing the required array before modifying it
[]
testing the method result after modifying the required array content
[ 'test' ]
观察到的点是,
- 是的,如果你想用语法
module.exports = { }
导出它没关系,这不是什么大问题。 - 如果任何方法在所需文件之外修改此数组,它也会影响此处,这是因为
require
将是引用,而不是副本。
一种可能的解决方案是创建它的 JSON 副本,同时 requiring
如下:
const { testArray, getArray } = require('./test1');
const testArrayCopy = JSON.parse(JSON.stringify(testArray));
console.log('testing the required array before modifying it');
console.log(getArray());
testArrayCopy.push('test');
console.log('testing the method result after modifying the required array content');
console.log(getArray());
这是结果:
>node test2.js
testing the required array before modifying it
[]
testing the method result after modifying the required array content
[]
注意:JSON复制不会帮助您正确解析 DateTime。