For plain objects and arrays this is the convention:
const obj = Object.assign({}, original);
const arr = original.slice();
But even if in userland it didn't suit, the user would know why, and what was suitable, then override the default clone strategy in his module. Having supplied his proper strategy, clone would meet his precise needs, guaranteed.
That is to ask, can the user write cloneObject
, cloneArray
, cloneDate
, clonePerson
in a way he likes? The answer must be yes. Otherwise, it's saying the user's object is impossible to clone. If impossible, then the user created a complexity which prevents it and he can, if he wants and needs clone, remove that complexity.
But I've already explained that obj!
is just obj.clone
where clone
is a method the user controls like every other method.
I can understand someone saying, but I don't clone and so I don't see any value here. I'd then guess that programmer is not doing any kind of FP, is not using the functional core, imperative shell pattern, that they're probably just doing OOP. Because in the FP world, unless you have proper persistent types you're probably using objects and arrays (the basic reference types). And they can be used to do FP, well enough for certain.
When I hear "but that won't work because...," I think "I clone all the time, been doing it for years and it works just fine" and that's why I've found the responses perplexing. I mean if users had no way to override the defaults, I'd get it, but I thought I'd explained the default could be overridden.
Or maybe you're saying that having a default which doesn't work 100% of the time makes no sense. I guess I don't see why convention over configuration couldn't be applied here, even if JS isn't a framework. The default works the vast majority of the time, but change if you need.
It's really not a big deal I guess. I have this sense that FP has not truly caught on in JS yet, and, to be frank, I am more invested in the other proposal than this one. I think this is a value add for those doing FP in JS, but I could live without it.