A quick object-cloning routine in 139 bytes.
-
-
Save addyosmani/1136817 to your computer and use it in GitHub Desktop.
/* | |
* 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; | |
}; |
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} |
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. |
{ | |
"name": "objectClone", | |
"description": "A compact object cloning routine.", | |
"keywords": [ | |
"object", | |
"clone", | |
"cloning" | |
] | |
} |
<!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> |
alternatively, if you're going to cache Object.prototype.toString
, why not use that instead of the less-reliable instanceof
to suss out an array?
@jed you're absolutely right. Let me play around with the alternatives just to make sure they're able to pass the same basic tests at the end. I'll edit that readme right away. Cheers!
just noodling, but i was also wondering about an approach that piggybacks on JSON:
function(a){return JSON.parse(JSON.stringify(a))}
perhaps there's enough space in there to handle functions and undefineds too.
I think you can shorten Object.prototype.toString
to {}.toString
.
How about this one?
function c(a,b,c){b={};for(c in''+a!==a&&a)b[c]=c(a[c]);return typeof a!='object'?a:a instanceof Array?a.slice():b}
Does not work to the fullest for html nodes, otherwise it's fine - maybe we could utilize a clone method if available...
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.
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.
nice work, addy! would you mind replacing or removing the old README?
also, wouldn't
be just as reliable as
?