An open API service indexing awesome lists of open source software.

https://github.com/writetome51/get-object-copy

Returns new identical copy of object, including same prototype chain
https://github.com/writetome51/get-object-copy

clone copy deep-clone dupe instance literal object

Last synced: about 1 year ago
JSON representation

Returns new identical copy of object, including same prototype chain

Awesome Lists containing this project

README

          

# getObjectCopy\(object: T): T

Returns independent copy of `object`, with same prototype chain.

## Examples
```
// These are tests you can run:

export class TestClass {
prop0 = {propA: {propB: {propC: {propD: 'value'}}}};
prop1 = 1;
prop2 = 2;

doThis() {
return this.prop1 + this.prop2;
}
}

export class TestSubclass extends TestClass {
prop3 = this.doThis(); // 3
}

export class TestSubSubclass extends TestSubclass {
}

let original = new TestSubSubclass();
let copy = getObjectCopy(original);

// Make sure copy is instanceof the most distant parent:
if (copy instanceof TestClass) console.log('test 1 passed');
else console.log('test 1 FAILED');

// Make sure copy is instanceof its immediate class:
if (copy instanceof TestSubSubclass) console.log('test 2 passed');
else console.log('test 2 FAILED');

// Make sure the copy is not just a reference to the original:
if (copy !== original) console.log('test 3 passed');
else console.log('test 3 FAILED');

// Make sure it contains the inherited method:
if (copy.doThis && (copy.doThis() === copy.prop3)) console.log('test 4 passed');
else console.log('test 4 FAILED');

// Make sure deeply nested properties are included in the copy:
if (copy.prop0.propA.propB.propC.propD === 'value') console.log('test 5 passed');
else console.log('test 5 FAILED');
```

## Installation

You must have npm installed first. Then, in the command line:

```bash
npm i @writetome51/get-object-copy
```

## Loading
```js
import {getObjectCopy} from '@writetome51/get-object-copy';
```