Wikipedia:Reference desk/Archives/Computing/2016 September 18

{{#ifeq:{{PAGENAME}}|Special:Undelete| |{{#if:|

}} {{#ifeq:{{NAMESPACE}}|Wikipedia|{{#switch:{{NAMESPACE}}|= |
}}|{{error:not substituted|Archive header}}
}}}} {{#if:|
}}
width = "100%"
colspan="3" align="center" | Computing desk
width="20%" align="left" | < September 17

! width="25%" align="center"|<< Aug | September | Oct >>

! width="20%" align="right" |{{#ifexist:Wikipedia:Reference desk/Archives/Computing/2016 September 19|September 19|Current desk}} >

align=center width=95% style="background: #FFFFFF; border: 1px solid #003EBA;" cellpadding="8" cellspacing="0"
style="background: #5D7CBA; text-align: center; font-family:Arial; color:#FFFFFF;" | Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is {{#ifexist:Wikipedia:Reference desk/Archives/Computing/2016 September 28|an archive page|a transcluded archive page}}. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.

__TOC__

= September 18 =

Create an exact copy of an object in JavaScript

Stack Overflow's answers to the question "How do I clone an object in JavaScript?" are unsatisfactory. All posts asking that are marked as duplicates of [http://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object?noredirect=1&lq=1 these] [http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript?rq=1 two] questions and the top answers for them do not come close to creating full copies of objects (the JSON solution drops functions or use jQuery which also drops parts and is unacceptable for other reasons). All I'm looking for is a way to create an exact copy of any object in a way that is used like

var CopyObject = Clone(OriginalObject);
. The code doesn't have to be short, but it shouldn't use jQuery or require including external JavaScript code and it should work for any object. Can anyone solve this? — Melab±1 16:49, 18 September 2016 (UTC)

:You can look at [http://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object this]. The answer is that there is no way to create an exact copy of an object unless you know its type. If you know the type of an object you can copy attributes as described in the link above. Ruslik_Zero 19:51, 18 September 2016 (UTC)