> Le 24 sept. 2015 à 19:58, Andrea Giammarchi <andrea.giammar...@gmail.com> a 
> écrit :
> 
> 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)
>   );
> };
> ```

```js
Object.clone = function (src) {
    return Object.create(Object.getPrototypeOf(src), 
Object.getOwnPropertyDescriptors(src))
}
```

where  the definition of `Object.getOwnPropertyDescriptors()` is left as an 
exercise to the reader.


—Claude

_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to