return src.map((item)=>clone(item));
probably
return src.map(Object.clone);
would do, if it accepts only one argument.
It seems that HTML nodes are missing (through document.createXXX) and it
relies on `instanceof` which works for me but it doesn't as proposal 'cause
incompatible cross realms.
Last, but not least, one does not simply assign properties in JavaScript.
If you want to clone you don't want to miss possible get/set accessors in
the process, you don't want to miss getOwnPropertyNames and
getOwnpropertySymbols neither, so the last loop doesn't work as proposal.
```js
// simplified version for regular objects only
// basically just the correct substitute of your last temp logic
Object.clone = function (src) {
return Object.setPrototypeOf(
Reflect.ownKeys(src).reduce((o, k) => {
var d = Object.getOwnPropertyDescriptor(src, k);
if (
o.hasOwnProperty.call(d, 'value') &&
/function|object/.test(typeof d.value)
) d.value = Object.clone(d.value);
return Object.defineProperty(o, k, d);
}, {}),
Object.getPrototypeOf(src)
);
};
```
That should be kinda better, IMO, and if you need to test with Reflect you
can:
```js
var Reflect = Reflect || {
ownKeys: (o) => Object.getOwnPropertyNames(o).concat(
Object.getOwnPropertySymbols(o)
)
};
```
Best Regards
On Thu, Sep 24, 2015 at 6:15 PM, Jeremy Darling <[email protected]>
wrote:
> I'm horrid with proposals, but this seems like a missing hole and I
> couldn't find a previous mention of it.
>
> Looking at the Object support methods it seems that the only thing that
> doesn't exist is a (simple) way to clone an object and all of its members.
>
> Ideally would allow creation of immutable and/or deep/shallow clones of
> existing Object/Array/RegExp/Date/...
>
> Basic shim (most likely not perfect):
>
> Object.clone = function(src){
> if(!src || typeof(src) !== 'object'){
> return src;
> }
> if(src instanceof Date){
> return new Date(src);
> }
> if(src instanceof Array){
> return src.map((item)=>clone(item));
> }
> if(src instanceof RegExp){
> return new RegExp(src);
> }
> var temp = (src.prototype)?Object.create(src.prototype):new
> src.constructor();
> Object.keys(src).forEach((key)=>{temp[key] = Object.clone(src[key]);});
> return temp;
> };
>
> Thoughts?
> - Jeremy
>
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss