I'm making a small example to display JSON (key and value). When I copy the JSON to another variable, I've tried to delete key Age
and re-displayed the first JSON. Key Age
in the first JSONhas been deleted, too.
$('button').click(function () {
var json = {};
json['Name'] = 'Hermione';
json['Age'] = 19;
for (i in json) {
$('body').append($('<p>').text(i + ': ' + json[i]))
}
var copy = json;
delete copy['Age'];
for (i in json) {
$('body').append($('<p>').text(i + ': ' + json[i]))
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click me</button>
So, my question is: Copying an object to another object means nothing in JavaScript, right?
I think that because when I copied it, it's still operating on the main version (json
), not on the copy version (copy
).