Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

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?

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.

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.

like image 863
Chad DeShon Avatar asked Dec 22 '09 20:12

Chad DeShon


People also ask

How do you assign one object to another in JavaScript?

var clone = Object. assign({}, obj); The Object. assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object.

What does += mean in JavaScript?

The addition assignment operator ( += ) adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator.

How do you assign a value to an object property?

Object.assign() Method Among the Object constructor methods, there is a method Object. assign() which is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target.

What is object equality in JavaScript?

If the two values have the same type, they are considered equal. == (double equals) is the loose equality operator. It converts both the values to a common type and then checks for equality. object.is() function.


2 Answers

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

var newObject = JSON.stringify(oldObject);

Then when I need to use it:

var evenNewerObj = JSON.parse(newObject);

Hope this helps someone.

like image 121
Matt Cashatt Avatar answered Sep 21 '22 07:09

Matt Cashatt


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

like image 28
Annie Avatar answered Sep 19 '22 07:09

Annie