Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/evanpipta/object-clone
npm package for cloning js objects
https://github.com/evanpipta/object-clone
Last synced: about 2 months ago
JSON representation
npm package for cloning js objects
- Host: GitHub
- URL: https://github.com/evanpipta/object-clone
- Owner: evanpipta
- License: gpl-3.0
- Created: 2015-12-09T22:45:34.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-06-26T22:02:10.000Z (over 8 years ago)
- Last Synced: 2024-10-12T05:28:01.387Z (3 months ago)
- Language: JavaScript
- Size: 485 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# object-clone
object-clone is a small package for cloning js objects### Introduction
Normally when you assign an object to a new variable in Javascript, the original variable and the new variable will both point to the same object in memory. So for example:
```
var x = { y: { z: 0 } };
var test = x;
test.y.z = 2;
// x.y.z === 2
```We don't always want the above behavior, and sometimes it's very inconvenient. This package adds an Object.clone method which returns a totally new object, not a reference to the original.
###Usage
```
require('object-clone');var x = { y: { z: 0 } };
var test = Object.clone( x );
test.y.z = 2;
// test.y.z === 2
// x.y.z === 0
```###Notes
* Test cases have been added in version 1.0.0 so it should be relatively reliable now
* Passing an object with circular references into Object.clone will now throw a TypeError instead of looping forever###Version Updates
* 1.0.0:
* Added fixed and random tests
* Fixed a bug that would attempt to call a constructor for null values
* Added error handling for circular referenes
* 0.1.2:
* Enabled strict mode and fixed a bug where the key variable in the for..in loop was put in the global scope