-1

I thought delete will remove the object property from a particular object. In the below example I am assigning d object to c and deleting property from c object. But the property got removed from both c and d.

var d = {a:1,b:2,c:3};
var c = d;
delete c.c;
console.log(d);

which returns Object { a=1, b=2}.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • 1
    I think this should be closed too, but the core issue really has nothing to do with parameter passing. There are absolutely no function calls involved here. – Pointy May 10 '14 at 05:31
  • @Pointy can you find a better reference? – John Dvorak May 10 '14 at 05:32
  • 1
    @JanDvorak I can search too, but it's painful. The problem is that most questions on this topic are posed by people with absolutely no experience with programming, or with terrible confusion about the nature of the language. Therefore, the question titles tend to be absolutely terrible. (*edit* I'm using the word "absolutely" a lot tonight :) – Pointy May 10 '14 at 05:33
  • @Pointy does this mean you are not going to vote to close? – John Dvorak May 10 '14 at 05:34
  • 1
    @JanDvorak give me a sec to do some searching :) I believe in closing questions, but I think it's wrong to close a question as a duplicate of an unrelated issue. – Pointy May 10 '14 at 05:34
  • @Pointy: Is assigning to a function parameter and assigning to a variable really all that different? – cookie monster May 10 '14 at 05:47
  • @cookiemonster well, yes; there are pass-by-reference semantics in languages that have value assignments. In this case it's probably just splitting hairs however. – Pointy May 10 '14 at 11:57
  • Yeah I meant specifically in JavaScript and as it relates to the other question. But good point. – cookie monster May 10 '14 at 14:02

2 Answers2

1

There is only one object in that code. Objects are assigned by reference in Javascript, so when you write c = d, then c and d both refer to the exact same object. Perhaps this is more evident when you modify a property, rather than delete it:

var d = {a:1,b:2,c:3};
var c = d;
c.c = 0;
console.log(d); Object {a: 1, b: 2, c: 0} 

If you want a copy of an object you need to create a new object that is identical. There are plenty of good answers about that here: What is the most efficient way to deep clone an object in JavaScript?

Community
  • 1
  • 1
Paul
  • 139,544
  • 27
  • 275
  • 264
  • "There are plenty of good answers" - then vote to close instead of answering (the upvote isn't mine, btw) – John Dvorak May 10 '14 at 05:30
  • @JanDvorak Those good answers aren't answering the OP's question, they are answering how to make a copy of an object in Javascript. – Paul May 10 '14 at 05:32
  • 1
    There are plenty of good answers to the original question already as well, I believe, aren't there? – John Dvorak May 10 '14 at 05:33
0

After var c = d, c and d point to the same set of memory, so making a change to one will affect the other. If you want to copy the value of an object, you have to copy it.

Meredith
  • 844
  • 6
  • 17