Javascript 使用Javascript中的赋值运算符将一个对象设置为另一个对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1948809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:01:57  来源:igfitidea点击:

Setting one object equal to another object with the assignment operator in Javascript

javascriptobjectvariable-assignment

提问by Chad DeShon

I'm coming to javascript from C background. In javascript, when I use the assignment operator to assign one object to another, does it copy the values from one to the another, or do they both now point to the same data?. Or does the assignment operator do anything in this case?

我从 C 背景来到 javascript。在 javascript 中,当我使用赋值运算符将一个对象分配给另一个对象时,它是将值从一个复制到另一个,还是它们现在都指向相同的数据?或者赋值运算符在这种情况下会做什么?

function point_type()
 {
 this.x = 0;
 this.y = 0;
 }

var pnt1 = new point_type();
var pnt2 = new point_type();

pnt1.x = 4;
pnt1.y = 5;

pnt2 = pnt1;

pnt1.x = 8;
pnt2.y = 9;

In the example above, does pnt2.x now equal 8, or does it still equal 4, or does it still equal 0?

在上面的例子中,pnt2.x 现在等于 8,还是等于 4,还是等于 0?

Yes, I realize I can test this myself, and I will be doing that while I wait for the community to come up with an answer. However, I'm hoping the answer to my question will go one step past just answering this one example and might shine some light on how javascript objects work and some best practices.

是的,我意识到我可以自己测试这个,我会在等待社区提出答案的同时这样做。但是,我希望我的问题的答案将比回答这个示例更进一步,并且可能会对 javascript 对象的工作方式和一些最佳实践有所启发。

Follow up question:
The answer seems to be that the reference is copied. pnt2 and pnt1 now point to the same data. Is it possible to set up my object so that the values are copied? How is this usually accomplished in javascript? Clearly I don't want to set each attribute individually every time I need to copy this object.

后续问题:
答案似乎是引用被复制。pnt2 和 pnt1 现在指向相同的数据。是否可以设置我的对象以便复制值?这通常是如何在 javascript 中完成的?显然,我不想在每次需要复制此对象时单独设置每个属性。

回答by Matt Cashatt

Whenever I need to copyone object to another in JS, I just cast it to a primitive:

每当我需要在 JS 中将一个对象复制到另一个对象时,我只需将其转换为原始对象:

var newObject = JSON.stringify(oldObject);

Then when I need to use it:

然后当我需要使用它时:

var evenNewerObj = JSON.parse(newObject);

Hope this helps someone.

希望这可以帮助某人。

回答by Annie

In JavaScript, primitive types are copied by value and reference types are copied by reference. More info here: http://docstore.mik.ua/orelly/web/jscript/ch09_03.html

在 JavaScript 中,原始类型按值复制,引用类型按引用复制。更多信息:http: //docstore.mik.ua/orelly/web/jscript/ch09_03.html

回答by Lark

It equals 8.

它等于 8。

pnt2 = pnt1

That statement is pointing the pnt2 object to the pnt1 object so any modification you do to pnt1 will show up in pnt2.

该语句将 pnt2 对象指向 pnt1 对象,因此您对 pnt1 所做的任何修改都将显示在 pnt2 中。

回答by Gabriel McAdams

Given the object you showed in your example, it is setting a reference to the object. If it were a primitive type (number, date) then it would copy the object.

鉴于您在示例中显示的对象,它正在设置对该对象的引用。如果它是原始类型(数字、日期),那么它将复制对象。