1. var china = {
    2. nation : '中国',
    3. birthplaces:['北京','上海','广州'],
    4. skincolr :'yellow',
    5. friends:['sk','ls']
    6. }
    7. //深复制,要想达到深复制就需要用递归
    8. function deepCopy(o,c){
    9. var c = c || {}
    10. for(var i in o){
    11. if(typeof o[i] === 'object'){
    12. //要考虑深复制问题了
    13. if(o[i].constructor === Array){
    14. //这是数组
    15. c[i] =[]
    16. }else{
    17. //这是对象
    18. c[i] = {}
    19. }
    20. deepCopy(o[i],c[i])
    21. }else{
    22. c[i] = o[i]
    23. }
    24. }
    25. return c
    26. }
    27. var result = {name:'result'}
    28. result = deepCopy(china,result)
    29. console.dir(result)

    json

    1. var test ={
    2. name:{
    3. xing:{
    4. first:'张',
    5. second:'李'
    6. },
    7. ming:'老头'
    8. },
    9. age :40,
    10. friend :['隔壁老王','宋经纪','同事']
    11. }
    12. var result = JSON.parse(JSON.stringify(test))
    13. result.age = 30
    14. result.name.xing.first = '往'
    15. result.friend.push('fdagldf;ghad')
    16. console.dir(test)
    17. console.dir(result)