Get Off My Lawn Get Off My Lawn - 1 year ago 68
TypeScript Question

Object is created but seems to be sharing a reference

I am building an editor, in the editor when you press play it converts a bunch of objects to a

(which explains to my engine how to build an object). When I then stop them I would like to rebuild the
again and it should be back to its original state (before play was pressed). The issue that I am having is that when
is pressed the items don't go back to their original state. They seem to stay in the state that they are in when stop was pressed.

Here is the class that I am using to convert Prefabs back and forth:

class Prefab {

public name: string;
public components: PrefabComponent[] = [];

public static create(gameObject: GameObject): Prefab {
let prefab = new Prefab; =;
gameObject.components.forEach(comp => {
let prefabComp = new PrefabComponent; =;
for (let c in comp) {
let prefabProp = new PrefabProperty; = c;
if (typeof comp[c] == 'object') {
prefabProp.value = Object.create(comp[c]);
} else {
prefabProp.value = comp[c];
return prefab;

public static toObject(prefab: Prefab): GameObject {
let gameObject = new GameObject(;
prefab.components.forEach(comp => {
let newComp;
if ( != 'transform') {
newComp = gameObject.addComponent(;
if (!newComp) {
newComp = gameObject.getComponent(;
} => {
if (prop.value instanceof Sprite) {
newComp[] = Sprite.create(prop.value.path);
} else if (typeof prop.value == 'object') {
newComp[] = Object.create(prop.value);
} else {
newComp[] = prop.value;
return gameObject;


class PrefabComponent {

public name: string;
public properties: PrefabProperty[] = [];


class PrefabProperty {

public name: string;
public value: any;


Here is how I am starting and stopping the game:

play.addEventListener('click', (event) => {
// Game has started
// Stop was pressed
if (game instanceof SpyNginMain) {
game = null;
// Reset the editor
prefabs.forEach(prefab => {
// Game has not started
// Play was pressed
else {
game = new SpyNginMain();
prefabs = [];
EditorObjectManager.items.forEach(item => {
game.init(scene, prefabs);

The issue seems to be when I try to convert Object with the
class. I think that it isn't creating an instance but is creating a reference to the original, but I am not sure...

Answer Source

Your create method will certainly retain references to the original object because of the following code:

    if (typeof comp[c] == 'object') {
        prefabProp.value = Object.create(comp[c]);
    } else {
        prefabProp.value = comp[c];

The Object.create call creates a new object with the original object as its prototype, so if you subsequently add/modify/delete a property in the original object, the changes will also appear in the newly created object.

I think what you meant to do here is to clone the original object? There are many existing implementations for cloning (eg lodash's cloneDeep).

I would replace all five lines quoted above with a single clone(comp[c]) call, since your typeof check is not reliable in other ways too (eg typeof null == 'object' is true, typeof [1,2,3] == 'object' is true, etc).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download