make Ox.clone() return a deep (not shallow) copy

This commit is contained in:
rlx 2011-08-24 02:00:00 +00:00
parent 9dd695b6ca
commit a8b9e1fd2c

View file

@ -289,15 +289,21 @@ Ox.avg = function(obj) {
};
/*@
Ox.clone <f> Returns a (shallow) copy of an object or array
Ox.clone <f> Returns a (deep) copy of an object or array
> (function() { a = ['val']; b = Ox.clone(a); a[0] = null; return b[0]; }())
'val'
> (function() { a = {key: 'val'}; b = Ox.clone(a); a.key = null; return b.key; }())
'val'
@*/
Ox.clone = function(obj) {
return Ox.isArray(obj) ? obj.slice() : Ox.extend({}, obj);
Ox.clone = function(col) {
// return Ox.isArray(col) ? col.slice() : Ox.extend({}, col);
var clone = Ox.isArray(col) ? [] : {};
Ox.forEach(col, function(val, key) {
clone[key] = ['array', 'object'].indexOf(Ox.typeOf(val)) > -1
? Ox.clone(val) : val;
});
return clone;
};
/*@