I want to write a cloneDeep
function that clones any JS object. There are two distinct problems here:
- Self references.
- Exotic objects.
I want to focus on the latter. Exotic objects in ECMAScript are listed in this section: 10.4 Built-in Exotic Object Internal Methods and Slots. There, it mentions exotic objects like strings, bound functions, Arrays etc. Specifically, it does not mention RegExp
.
So, my expectation was that given RegExp
is an ordinary object, if I copy all its own properties (enumerable and non-enumerable) and its prototype, it will behave like the source RegExp
object. But that is not true: it fails when I try to access the getter regex.source
by explicitly saying it is not a RegExp
object.
Digging further, I found that some user-inaccessible internal slots exist in ECMAScript for various objects only to be accessible through their constructors.
This leads me to believe either of the two is true:
RegExp
is also an exotic object somehow.- It is an ordinary object but there is some other state I did not copy.
Which one of these is true? Because I am not able to wrap my head around an ordinary object with object-specific internal slots.