Socket
Book a DemoInstallSign in
Socket

object-clone

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-clone

Adds a clone method to duplicate objects

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

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;
// 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

FAQs

Package last updated on 26 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts