Path: csiph.com!.POSTED.208-71-239-109.cust.centrio.cz!not-for-mail From: Cezary Tomczyk Newsgroups: comp.lang.javascript Subject: Re: Cloning an object Date: Thu, 5 Jul 2018 23:54:31 +0200 Organization: csiph.com Internet News Service Message-ID: References: <1620852.VI2in3QvqR@PointedEars.de> <9909bfec-c6e7-2a4a-2a54-ec9f271691c8@PointedEars.de> <4395113.31r3eYUQgx@PointedEars.de> <506b67da-b086-3d0e-5985-d0f8214da432@PointedEars.de> <5399415.lOV4Wx5bFT@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 5 Jul 2018 21:54:32 -0000 (UTC) Injection-Info: csiph.com; posting-host="208-71-239-109.cust.centrio.cz:109.239.71.208"; logging-data="28293"; mail-complaints-to="admin@kev009.com" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 In-Reply-To: <5399415.lOV4Wx5bFT@PointedEars.de> Content-Language: en-US Xref: csiph.com comp.lang.javascript:35355 On 05/07/2018 23:35, Thomas 'PointedEars' Lahn wrote: > Cezary Tomczyk wrote: > >> On 05/07/2018 19:49, Thomas 'PointedEars' Lahn wrote: >>> Cezary Tomczyk wrote: >>>> On 04/07/2018 23:25, Thomas 'PointedEars' Lahn wrote: >>>>> function clone (obj, deepCopy) > ^^^^^^^^ >>>>> { >>>>> […] >>>>> Object.getOwnPropertyNames(obj).forEach((name) => { >>>>> […] >>>>> if (deepCopy) > ^^^^^^^^ >>>>> { >>>>> /* FIXME: Standardize Object.isObject() */ >>>>> if (/* […] */) >>>>> { >>>>> DESC.value = clone(DESC.value); > ^^ >>>>> } >>>>> […] >>>> It passes for simple objects, but if the property has a value as an >>>> array or Date object then it doesn't work. >>> I agree regarding Date instances, but where is the problem with Array >>> instances? >> >> It doesn't create a real Array copy. Try clone following object: >> >> […] >> var cloned = clone(o); > ^^ >> […] >> >> This test doesn't pass. Basically, modifications on original object >> should not modify cloned object. > > In a way it both works as designed and in another it is b0rken as designed: > > You forgot to tell clone() to make a deep copy – as did I in the code above > (but not in my later tests) :) No, I didn't. I use a bit different way of making a copy of object and I've used your approach to see if it passes all unit tests I have. Because of not passing tests I've shared here the results. The only way that works to me is to use "map" and create a new Array. Then object in the array item is like a real copy. -- Cezary Tomczyk http://www.ctomczyk.pl/ https://www.aslint.org/