0
<p id="display1"></p>
<p id="display2"></p>



var person1= {
    name: 'John Smith',
    hobbies: ['surfing', 'diving']
};

$("#display1").append( "<br/>" + "person name :" + person1.name  + "<br/>");

var person2= clone(person1);

$("#display2").append( "<br/>" + "person name :" + person1.name  + "<br/>");

Demo jsFiddle

the above code siplay person1 name.

why it doesn't display person2 ? Many thx.

yossi
  • 1,431
  • 2
  • 11
  • 16

5 Answers5

2

There is a syntax error in your code. clone clones the DOM objects, why not?

var person2 = person1;
Ram
  • 143,282
  • 16
  • 168
  • 197
1

JQuery's clone() only clone DOM elements. Use extend() instead : $.extend({}, person1);

slaphappy
  • 6,894
  • 3
  • 34
  • 59
1

check out the answer to this question

What is the most efficient way to deep clone an object in JavaScript?

covers what you need, which is shown in this fiddle

http://jsfiddle.net/sGK6u/1/

Community
  • 1
  • 1
olly_uk
  • 11,559
  • 3
  • 39
  • 45
0
$("#display2").append( "<br/>" + "person name :" + person2.name  + "<br/>");
Sid
  • 854
  • 9
  • 23
0

You are getting error in Clone()...Use "extend" method to copy the object

 jQuery.extend(copiedObject,originalObject);
Amol Kolekar
  • 2,307
  • 5
  • 30
  • 45