A quick object-cloning routine in 139 bytes.
-
-
Save addyosmani/1136817 to your computer and use it in GitHub Desktop.
140byt.es -- objectClone
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
/* | |
* objectClone.js (c) Addy Osmani, 2011. | |
* Do whatever license. | |
* Thanks to gf3 and ben_alman for tips that helped improve. | |
*/ | |
function objectClone(q) { | |
/*determine if the object is an instance of a known object type (array).if an array, instantiate n as an array*/ | |
var n = (q instanceof Array) ? [] : {}, | |
i; | |
/*iterate through the input*/ | |
for (i in q) { | |
/*Object.prototype.toString is a ref to Object.prototype so the results [object Object] will result despite argument supplied. call() however sets toString's 'this' keyword as a ref to the passed input (eg. [object Array]) allowing us to accurately handle different types of input*/ | |
if (Object.prototype.toString.call({}) == Object.prototype.toString.call(q[i])) { | |
/*if an object, set n to a clone of q[i]*/ | |
n[i] = objectClone(q[i]); | |
/*otherwise, simply map n[i] to q[i] (eg. for arrays)*/ | |
} else n[i] = q[i] | |
} | |
return n; | |
}; |
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 c(a){var b=a instanceof Array?[]:{},d,e=Object.prototype.toString;for(d in a)b[d]=e.call({})==e.call(a[d])?c(a[d]):a[d];return b} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "objectClone", | |
"description": "A compact object cloning routine.", | |
"keywords": [ | |
"object", | |
"clone", | |
"cloning" | |
] | |
} |
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> | |
<title>Foo</title> | |
<script> | |
function c(a){var b=a instanceof Array?[]:{},d,e=Object.prototype.toString;for(d in a)b[d]=e.call({})==e.call(a[d])?c(a[d]):a[d];return b} | |
/*test cloning*/ | |
var obj = { omg: 'wow', sexypants : 'mikeyface', tester:function(){ console.log('ZOMGAH');}}, | |
test = c(obj); | |
console.log(obj); | |
console.log(test); | |
/*test direct assignment, followed by cloning*/ | |
test.p = 'lalalalalala'; | |
test.magic = function(){ | |
console.log('magicalness'); | |
} | |
test.tester(); | |
console.log(test); | |
/*test passing arrays through the cloner*/ | |
var testArray = ['beans','beans','the','magical','fruit'], | |
testObj = c(testArray); | |
console.log(testArray); | |
console.log(testObj); | |
</script> |
Function(func+'')
- does not work for native functions.
sure, so what is a cloned function? i guess in a pure sense it would be a proxy, but that would kill any hopes of it fitting in 140 bytes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i guess i'm just not clear on what a deep clone should be: should the source and target contain any objects in common?
primitives are easy, and objects are less easy, but functions? you can't really clone a function reliably.