How to Clone/Copy class with local stuff
DCWizard

DCWizard @dcwizard

Joined:
Jan 17, 2023

How to Clone/Copy class with local stuff

Publish Date: Apr 11
0 4

As I was implementing my undo in my project I stumble upon a problem. When I would clone or copy a class Object even with a deep clone.
It will not clone the local variables. Well that's a problem and would even say that's a flaw in JS if it's not a bug. (Like the imaginary math I suppose.)

In case you don't know. A local var of a class in JS is a like a member that doesn't have the prefix this. in front of it.
Example:

class MultiEpisode {
  constructor(ThisFilename = "", ThisMediaText = ""){
    let MeBeLocalObj        = {"SomeObject":GetElementById("Whatever")};
    let MeBeLocalArray      = [1,2,3];
    let MeBeLocalVar        = 10; 
    let MeBeLocalString     = "Hello Morons"; 

    // NOW this "MeBeLocal" will simply not exist in a normal Cloning/copy.
    // It will simply point to the original. So changing the state of one is applied to all cloned 
    // That has the potential to be a major issue. How come nobody ever consider that. 
    // The only way this "MeBeLocal" will exist is by making a new instance of that Class.
    // A cloning won't do. Just keep this in mind.
  } 
}
Enter fullscreen mode Exit fullscreen mode

What's The solution:
The best way I found and let me know if you have a better idea.
Is to have a public member that Clone/Copy itself.
Basically, you also need a Public function that will export all the internals/locals.
In other word: Make them public sort of.
Something that would look like this:

    this.GetInternals                 = function (){
      return {MeBeLocalObj, MeBeLocalArray, MeBeLocalVar, MeBeLocalString};
    }
    this.CloneInternals               = function (That){
      let ThatInternals               = That.GetInternals();
      MeBeLocalObj                  = ThatInternals.MeBeLocalObj;
      // This will only point to the original, if you want a real copy
      MeBeLocalObj                  = CopyObject(ThatInternals.MeBeLocalObj);
      MeBeLocalArray                = CopyArray(ThatInternals.MeBeLocalArray);
      // Or if that object also a Complex class with lots of locals
      //  you may have to implement it's cloning too.
      MeBeLocalObj                  = ThatInternals.MeBeLocalObj.Clone();  
      // And if this go on. You will have hours of fun if not days, ahead of you. 


      // These do not need a full copy. Already a copy.
      MeBeLocalVar                  = (ThatInternals.MeBeLocalVar);
      MeBeLocalString               = (ThatInternals.MeBeLocalString);
    }  
    this.Copy                         = function(){
      let ThisClone                   = new MultiEpisode(ThisFilename);
      ThisClone.$CLONED               = true;
      ThisClone.CloneInternals         (this);
      return ThisClone;
    }
Enter fullscreen mode Exit fullscreen mode

I am assuming you know how to do a simple CopyObject and a simple CopyArray.
If you don't and need a full working example. Let me know in the comments and I'll make a CodePen about it.

Comments 4 total

  • Matt Ellen-Tsivintzeli
    Matt Ellen-TsivintzeliApr 11, 2025

    Those are not private member variables as they are not members of the class. They are local variables, scoped to the constructor.

    • DCWizard
      DCWizardApr 12, 2025

      Yes, you're right then you tell me how to create a private function and variable? If there is another way.
      And they also act as static if you use a CloneObject... Affecting One affect the other.

      By CloneObject I mean : an Object.assign() + Member loop.

      One more thing. The so called javascript class private member kinda sucks in so many ways.
      I'm offering a clean/clear alternative. Which is far better than those JS dev. ever came up with.

      • Matt Ellen-Tsivintzeli
        Matt Ellen-TsivintzeliApr 15, 2025

        Private properties. This what I found in the MDN documentation.

        Does it solve your problem?

        • DCWizard
          DCWizardApr 15, 2025

          Thanks, yeah, I found it "search for js class private." after my post. This always happen to me.
          Tried it, and unfortunately doesn't solve my problems. It made it worst.
          And still cannot work when I tried to use a Object.assign on instance of it.

          Also the code look ugly as f. I'm an old C/C++ I'm not used to put this. everything inside the class. I find this disgusting. The locals do a very good jobs of acting like a super private.

          So what do you think of my solution?
          But I'm gonna remove the private part in my title.

Add comment