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

大兔子大兔子 提交于 2019-11-27 14:06:10

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

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.

It equals 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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!