简体   繁体   English

如何防止更改模块函数返回的对象而更改Javascript中的模块数据本身?

[英]How can I prevent changes on the objects returned by the module functions from changing the module data itself in Javascript?

I am working with Node for the first time and I have two modules. 我是第一次使用Node,我有两个模块。 One module defines an array of objects, and also has functions that are exported for use in the other module - including lookupbyID, lookupbyLastName, and addEmployee. 一个模块定义了一个对象数组,还具有导出的功能以供另一个模块使用-包括lookupbyID,lookupbyLastName和addEmployee。

My issue is that when I call the functions from module 1 in module 2, which return objects from the original array and assign those objects to a variable, and then I modify that variable, it modifies the original data. 我的问题是,当我从模块2中的模块1调用函数时,该函数从原始数组返回对象并将这些对象分配给变量,然后修改该变量,它将修改原始数据。 Please see the following code: 请参见以下代码:

Module 1: 模块1:

const us = require('underscore')
var data = [
  {id:1, firstName:'John', lastName:'Smith'},
  {id:2, firstName:'Jane', lastName:'Smith'},
  {id:3, firstName:'John', lastName:'Doe'},
]

exports.lookupByID = function (given_id) {
  var found_id = us.findWhere(data, {id:given_id});
  return found_id;
}

Module 2: 单元2:

const employeeFunctions = require("./employeeModule");
var id_2_answer = employeeFunctions.lookupByID(2);
id_2_answer.firstName = 'Mary'
console.log(employeeFunctions.lookupByID(2))

As you can see, I changed the name of Jane to Mary. 如您所见,我将Jane的名字更改为Mary。 Even though I assigned the object to a variable, changing the variable changed the original object data, which I verified by printing the lookupbyID function a second time. 即使我将对象分配给变量,更改变量也改变了原始对象数据,我通过第二次打印lookupbyID函数进行了验证。

Can you help me understand why this happens? 您能帮我理解为什么会这样吗? Can you help me understand possible ways to prevent this from happening? 您能帮助我了解防止这种情况发生的可能方法吗? I would like to be able to assign the object to a variable, and be able to change the values within the variable without affecting the original data. 我希望能够将对象分配给变量,并能够在不影响原始数据的情况下更改变量内的值。

Thank you! 谢谢!

In js objects are assigned by reference so: 在js中,对象是通过引用分配的,因此:

var foo = {bar:1};
var baz = foo;
baz.bar; // 1
baz.bar = 2;
foo.bar; // 2

Use Object.assign to clone objects instead: 使用Object.assign克隆对象:

var foo = {bar:1};
var baz = Object.assign({}, foo);
baz.bar; // 1
baz.bar = 2;
foo.bar; // 1

The short answer is that you can't - you've hit on a key aspect of programming: value vs reference. 简短的答案是,您不能-碰到了编程的一个关键方面:价值与参考。 You can read more about it here but the long and short of it is that both of these variables point to the same object in memory, so changing it for one changes it for the other. 您可以在此处阅读有关它的更多信息,但总的来说,这两个变量都指向内存中的同一对象,因此将其更改为一个将更改为另一个。

The solution will necessarily involve creating a new object - the neatest way is probably to use Object.assign to copy over values, but bear in mind that in your example, you'll probably need to delete your old entry too. 解决方案将必然涉及创建一个新对象-最简洁的方法可能是使用Object.assign复制值,但请记住,在您的示例中,您可能还需要删除旧条目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 JavaScript 对象的所有功能导出为模块,而无需对文件进行大量更改? - How can I export all functions of a JavaScript object as module without having to make a lot of changes to the file? 如何从另一个模块JS对象中的JavaScript对象访问函数和变量? - How can I access functions and variables from my JavaScript object inside of another module JS object? 如何使用php中的javascript函数返回的值? - how can i use returned values from javascript functions in php? 如果我的JavaScript函数返回false,我该如何阻止表单操作? - How can I prevent a form action if my JavaScript functions have returned false? 如何防止JavaScript函数自动运行? - How can I prevent JavaScript functions from autorunning? Dojo javascript 工具包,我在一个模块中定义了两个函数。 我怎么能从另一个打电话给一个 - Dojo javascript toolkit, i have two defined functions inside one module. How can i call one from another 如何覆盖从 javascript 中的模块返回的 class 中的 function - How to override a function in a class being returned from module in javascript 如何从 html onclick 中的 javascript 模块调用函数 - How can I call a function from javascript module in html onclick 如何将URL从Javascript传递到Java模块 - How can I pass a URL from Javascript to Java module 如何从JavaScript模块中访问“ this”? - How can I access “this” from within a JavaScript module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM