Created
September 8, 2013 08:32
-
-
Save kevadsett/6482949 to your computer and use it in GitHub Desktop.
Simple plain javascript object clone (not fully tested but it does as much as I need!).
This file contains hidden or 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 cloneObject(objectToClone) { | |
| var key, i, clonedArray = []; | |
| var clone = {}; | |
| if(typeof objectToClone == "object") { | |
| if(objectToClone.length == undefined) { | |
| for(key in objectToClone){ | |
| clone[key] = cloneObject(objectToClone[key]); | |
| } | |
| } else { // we're an array or string | |
| clone = objectToClone.slice(0) | |
| for(i = 0; i < clone.length; i++) { | |
| clone[i] = cloneObject(clone[i]); | |
| } | |
| } | |
| } else { // we're a number or some other basic datatype | |
| clone = objectToClone; | |
| } | |
| return clone; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment