Skip to main content

Copying an Object in JavaScript

The Setup

var default = { param1: "one", param2: "two", param3: [] },
    working = default;

// Fill form fields from working data
// User modifies form fields
// Save form fields in working data
// Perform calculation based on working data
// User clicks a button to reset the form to default state

function reset(){
  working = default;
  // Fill form fields from working data
  // Perform calculation based on working data
}

The Problem

The form was not being reset to the default data, but instead did not change at all.

When I debugged, I noticed that the data in default was identical to the data in working, even though I never assign any values to default after initialization.

The Cause

Little did I know, that assigning an object in JavaScript actually only assigns the reference, and does not create a new copy of the object.

The Solution

The object needs to be copied, rather than simply assigned. Based on a quick Google search, there is no built-in method to do this. But I did find a great post on Stack Overflow explaining the issue and how to copy one object to another.

I ended up using the function provided by A. Levy, which I'll post here for convenience (Though I removed the Date section, since I would not be needing it).

function clone(obj) {
    // Handle the 3 simple types, and null or undefined
    if (null == obj || "object" != typeof obj) return obj;

    // Handle Date
    if (obj instanceof Date) {
        var copy = new Date();
        copy.setTime(obj.getTime());
        return copy;
    }

    // Handle Array
    if (obj instanceof Array) {
        var copy = [];
        for (var i = 0, var len = obj.length; i < len; ++i) {
            copy[i] = clone(obj[i]);
        }
        return copy;
    }

    // Handle Object
    if (obj instanceof Object) {
        var copy = {};
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
        }
        return copy;
    }

    throw new Error("Unable to copy obj! Its type isn't supported.");
}

I had actually just written a similar function to recursively step through an object and convert it to an array, so I could have written this myself, however the code was right there in the article that explained why I was having this problem, and so much easier to just copy and paste. Plus I didn't know about the hasOwnProperty method, which makes this code better than mine would have been.

Lesson Learned

Unless you want to assign a pointer to an object, you will need to clone, or copy, the object into the new variable.

Use the hasOwnProperty method when looping through the properties of an object to only copy local attributes

Comments

Popular posts from this blog

CSS line-height Units Explained

In Chris Coyier's recent article, " Fun with line-height! ", he mentions that we often use unitless values for line-height so that it's proportional to the font size. This is only part of the story, and I was reminded of a Stack Overflow question that got me investigating more about how line-height worked with regards to the various CSS units. If you're declaring the line-height on each element, you won't notice any difference. But if you're not crazy and using the first C of CSS (that's 'Cascading', by the way), then the inherited line-height might not work the way you expect. Disclaimer: This is my understanding after doing some research and testing. I may not be completely correct. TL;DR When using a relative unit (em, %), the line-height is calculated based on the font size of the element where the line-height is defined. This line-height is then inherited, unchanged, by each of its descendants. So we end up with a line...

Naming Booleans

Naming conventions are one of those little things that can become a big thing when you multiply the scale of the project and/or people involved. I was just looking through some code yesterday, and I saw a variable with a fairly typical boolean name that followed the format isObjectState This felt awkward to me, and I took a moment to consider why this is. In Logic, a boolean is a statement that is either true or false . However, this variable name is written as a yes or no question. It is an easy misconception to equate true with yes and false with no , but though they are similar, they are not exactly the same, and, especially as programmers, we should not treat them as such. When we expand our variables into full sentences, the awkwardness becomes more apparent. Especially when we insert them into control structures. Let’s rename our variable to: is this menu item active? So our control blocks will read: if is this menu item active?, then highlight it. while is this men...

Get width and height of a remote image with VB .NET

The Problem I wanted to grab the width and height of an image that was on a remote server. I've done this with PHP, but never in .NET. The Solution I did a little digging and came across this code (slightly modified for my own purposes). Dim request As HttpWebRequest = DirectCast(WebRequest.Create(URL), HttpWebRequest) request.Method = "GET" request.Accept = "image/*" Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse) Dim s As Stream = response.GetResponseStream() Dim bmp As New Bitmap(s) Width = bmp.Width Height = bmp.Height