How to Deep clone in javascript

前端 未结 19 1568
-上瘾入骨i
-上瘾入骨i 2020-11-22 02:06

How do you deep clone a JavaScript object?

I know there are various functions based on frameworks like JSON.parse(JSON.stringify(o)) and $.extend(t

19条回答
  •  自闭症患者
    2020-11-22 02:55

    Avoid use this method

    let cloned = JSON.parse(JSON.stringify(objectToClone));
    

    Why? this method will convert 'function,undefined' to null

    const myObj = [undefined, null, function () {}, {}, '', true, false, 0, Symbol];
    
    const IsDeepClone = JSON.parse(JSON.stringify(myObj));
    
    console.log(IsDeepClone); //[null, null, null, {…}, "", true, false, 0, null]
    

    try to use deepClone function.There are several above

提交回复
热议问题