I guess I'll make a proposal myself, if nobody beats me to it. Here's the polyfill:
if (typeof Object.clone !== "function") { const clone = (obj, map) => { if (obj === null || typeof obj !== "object" || obj instanceof WeakMap) return obj; if (map.has(obj)) return map.get(obj); const temp = obj instanceof TypedArray ? new obj.constructor(obj.length) : new obj.constructor(); map.set(obj, temp); if (obj instanceof TypedArray) { temp.set(obj.map((value) => clone(value, map))); } else if (obj instanceof Map) { obj.forEach((value, key) => temp.set(key, clone(value, map))); } else if (obj instanceof Date) { temp.setTime(obj.getTime()); } else { for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { temp[key] = clone(obj[key], map); } } } return temp; }; Object.clone = (obj) => clone(obj, new Map()); }
I also did a small test suite and a documentation. I'll work a bit on it and then release it to the public.
Wow Alex, Your a speedy guy! Nice work, happy to test with you 👀
Here's the initial draft: github.com/atk/object-clone-proposal. Feedback is appreciated.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I guess I'll make a proposal myself, if nobody beats me to it. Here's the polyfill:
I also did a small test suite and a documentation. I'll work a bit on it and then release it to the public.
Wow Alex, Your a speedy guy!
Nice work, happy to test with you 👀
Here's the initial draft: github.com/atk/object-clone-proposal. Feedback is appreciated.