Last change
on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
979 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | export function klona(x) {
|
---|
| 2 | if (typeof x !== 'object') return x;
|
---|
| 3 |
|
---|
| 4 | var k, tmp, str=Object.prototype.toString.call(x);
|
---|
| 5 |
|
---|
| 6 | if (str === '[object Object]') {
|
---|
| 7 | if (x.constructor !== Object && typeof x.constructor === 'function') {
|
---|
| 8 | tmp = new x.constructor();
|
---|
| 9 | for (k in x) {
|
---|
| 10 | if (tmp.hasOwnProperty(k) && tmp[k] !== x[k]) {
|
---|
| 11 | tmp[k] = klona(x[k]);
|
---|
| 12 | }
|
---|
| 13 | }
|
---|
| 14 | } else {
|
---|
| 15 | tmp = {}; // null
|
---|
| 16 | for (k in x) {
|
---|
| 17 | if (k === '__proto__') {
|
---|
| 18 | Object.defineProperty(tmp, k, {
|
---|
| 19 | value: klona(x[k]),
|
---|
| 20 | configurable: true,
|
---|
| 21 | enumerable: true,
|
---|
| 22 | writable: true,
|
---|
| 23 | });
|
---|
| 24 | } else {
|
---|
| 25 | tmp[k] = klona(x[k]);
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 | return tmp;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | if (str === '[object Array]') {
|
---|
| 33 | k = x.length;
|
---|
| 34 | for (tmp=Array(k); k--;) {
|
---|
| 35 | tmp[k] = klona(x[k]);
|
---|
| 36 | }
|
---|
| 37 | return tmp;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | if (str === '[object Date]') {
|
---|
| 41 | return new Date(+x);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | if (str === '[object RegExp]') {
|
---|
| 45 | tmp = new RegExp(x.source, x.flags);
|
---|
| 46 | tmp.lastIndex = x.lastIndex;
|
---|
| 47 | return tmp;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | return x;
|
---|
| 51 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.