Created
January 15, 2018 12:08
-
-
Save fearmear/27b412d9292d1c2857e8059277bf9c77 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/nojatos
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
const deepClone = obj => { | |
if (!(obj instanceof Object)) { | |
return obj | |
} | |
const result = {...obj} | |
Object.keys(obj).forEach((key) => { | |
result[key] = deepClone(obj[key]) | |
}) | |
return result | |
} | |
const assert = (statement, successMsg) => { | |
const msg = statement ? `SUCCESS: ${successMsg}` : `ERROR: ${successMsg}. \nExpected ${statement} to be true` | |
console[statement ? 'info' : 'error'](msg) | |
} | |
const obj = {foo: {bar: {baz: 1}}} | |
console.clear() | |
assert(deepClone(obj) !== obj, 'First level object is a new object') | |
assert(deepClone(obj).foo !== obj.foo, 'Second level object is a new object') | |
assert(deepClone(obj).foo.bar !== obj.foo.bar, 'Third level object is a new object') | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
const deepClone = obj => { | |
if (!(obj instanceof Object)) { | |
return obj | |
} | |
const result = {...obj} | |
Object.keys(obj).forEach((key) => { | |
result[key] = deepClone(obj[key]) | |
}) | |
return result | |
} | |
const assert = (statement, successMsg) => { | |
const msg = statement ? `SUCCESS: ${successMsg}` : `ERROR: ${successMsg}. \nExpected ${statement} to be true` | |
console[statement ? 'info' : 'error'](msg) | |
} | |
const obj = {foo: {bar: {baz: 1}}} | |
console.clear() | |
assert(deepClone(obj) !== obj, 'First level object is a new object') | |
assert(deepClone(obj).foo !== obj.foo, 'Second level object is a new object') | |
assert(deepClone(obj).foo.bar !== obj.foo.bar, 'Third level object is a new object')</script></body> | |
</html> |
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
const deepClone = obj => { | |
if (!(obj instanceof Object)) { | |
return obj | |
} | |
const result = {...obj} | |
Object.keys(obj).forEach((key) => { | |
result[key] = deepClone(obj[key]) | |
}) | |
return result | |
} | |
const assert = (statement, successMsg) => { | |
const msg = statement ? `SUCCESS: ${successMsg}` : `ERROR: ${successMsg}. \nExpected ${statement} to be true` | |
console[statement ? 'info' : 'error'](msg) | |
} | |
const obj = {foo: {bar: {baz: 1}}} | |
console.clear() | |
assert(deepClone(obj) !== obj, 'First level object is a new object') | |
assert(deepClone(obj).foo !== obj.foo, 'Second level object is a new object') | |
assert(deepClone(obj).foo.bar !== obj.foo.bar, 'Third level object is a new object') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment