How to REALLY clone the prototype of an object in Javascript?

Tiago Bértolo
2 min readOct 11, 2022

--

I am going to publish an article soon with a comparison of the most famous deep clone methods in Javascript. I will evaluate these methods in terms of correctness and performance.

The results are really surprising in my opinion.

One of the most surprising facts is that all of the major deep cloning libraries and methods, except for structuredClone(), opt to not clone the prototype and simply create a reference to it.

Let me explain…

If I decide to add one function to the prototype of my cloned object I expect not to change the original object’s prototype.

In short, all the methods I studied, except for structuredClone(), fail this test, where I make a clone of an input object, add a function to the clone prototype and verify that the input prototype remains unchanged:

So I want to share with you a simple piece of code that shows how it is possible to clone the prototype of an object without just referencing it.

If you don’t understand my code please leave a comment and I will clarify.

Please remember that this code is just a hint of how this problem could be solved.
This code is not dealing with many other complex problems that deep cloning methods face solve such as dealing with circular dependencies, nesting, different types, etc.

--

--