mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-24 17:35:16 +00:00
53 lines
995 B
JavaScript
53 lines
995 B
JavaScript
|
function klona(x) {
|
||
|
if (typeof x !== 'object') return x;
|
||
|
|
||
|
var k, tmp, str=Object.prototype.toString.call(x);
|
||
|
|
||
|
if (str === '[object Object]') {
|
||
|
if (x.constructor !== Object && typeof x.constructor === 'function') {
|
||
|
tmp = new x.constructor();
|
||
|
for (k in x) {
|
||
|
if (tmp.hasOwnProperty(k) && tmp[k] !== x[k]) {
|
||
|
tmp[k] = klona(x[k]);
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
tmp = {}; // null
|
||
|
for (k in x) {
|
||
|
if (k === '__proto__') {
|
||
|
Object.defineProperty(tmp, k, {
|
||
|
value: klona(x[k]),
|
||
|
configurable: true,
|
||
|
enumerable: true,
|
||
|
writable: true,
|
||
|
});
|
||
|
} else {
|
||
|
tmp[k] = klona(x[k]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return tmp;
|
||
|
}
|
||
|
|
||
|
if (str === '[object Array]') {
|
||
|
k = x.length;
|
||
|
for (tmp=Array(k); k--;) {
|
||
|
tmp[k] = klona(x[k]);
|
||
|
}
|
||
|
return tmp;
|
||
|
}
|
||
|
|
||
|
if (str === '[object Date]') {
|
||
|
return new Date(+x);
|
||
|
}
|
||
|
|
||
|
if (str === '[object RegExp]') {
|
||
|
tmp = new RegExp(x.source, x.flags);
|
||
|
tmp.lastIndex = x.lastIndex;
|
||
|
return tmp;
|
||
|
}
|
||
|
|
||
|
return x;
|
||
|
}
|
||
|
|
||
|
exports.klona = klona;
|