Created
July 5, 2019 13:54
-
-
Save lusan/55b907ab5c47c4059a16ce7f522802c3 to your computer and use it in GitHub Desktop.
Deep clone object polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepClone(object){ | |
var newObject = {}; | |
for(var key in object){ | |
if(typeof object[key] === 'object' && object[key] !== null ){ | |
newObject[key] = deepClone(object[key]); | |
}else{ | |
newObject[key] = object[key]; | |
} | |
} | |
return newObject; | |
} |
it will not work for function, array values.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
let's say if
object[key]
is a function type, thennewObject[key]
is still pointing to the originalobject[key]
instead of clone to a new memory space