Please help. I think I am missing some basic funda here, but I am not able to catch it.

问题描述 投票:1回答:3

I am facing a strange problem in JavasSript. There are three variables (declared as var), they all store an object; say var object1, object2 and object3. Ok, so what I do now is I assign object2=...varSo how is that strange?var object1You pass reference to the object and do object2notobject3 clone the object. So any modification of your variables object1 to object3 actually refer to the same object, thus "automatically updating all variables".

object2=object1 object3=object1When you assign

, you are not making a copy of object1, you are simply asking that both names refer to the same object. You need to make a new object and copy the attributes from object1 into it. How you do that depends on what Javascript libraries you have available to you.object2jQuery provides copying techniques object3described here

javascript object reference
3个回答
2
投票

我在JavasSript中遇到一个奇怪的问题。在JavasSript中,有三个变量(声明为 ),它们都存储一个对象;比如说

,

2
投票

.opject2 = object1好了,我现在要做的是,我把我的任务分配给

.

而当我添加一些东西到

0
投票

. 奇怪?

© www.soinside.com 2019 - 2024. All rights reserved.