Path: csiph.com!.POSTED.208-71-239-109.cust.centrio.cz!not-for-mail From: Cezary Tomczyk Newsgroups: comp.lang.javascript Subject: Re: Recording changes to an object Date: Mon, 2 Jul 2018 16:29:49 +0200 Organization: csiph.com Internet News Service Message-ID: References: <1620852.VI2in3QvqR@PointedEars.de> <9909bfec-c6e7-2a4a-2a54-ec9f271691c8@PointedEars.de> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Mon, 2 Jul 2018 14:29:49 -0000 (UTC) Injection-Info: csiph.com; posting-host="208-71-239-109.cust.centrio.cz:109.239.71.208"; logging-data="51064"; 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: <9909bfec-c6e7-2a4a-2a54-ec9f271691c8@PointedEars.de> Content-Language: en-US Xref: csiph.com comp.lang.javascript:35340 On 01/07/2018 15:22, Thomas 'PointedEars' Lahn wrote: > Thomas 'PointedEars' Lahn wrote: > >> Making a copy of, i.e. cloning, an object is not trivial, though. Basic >> general approach (that I basically created yesterday in an unrelated matter): >> >> var o = …; >> >> var clone_of_o = Object.create(Object.getPrototypeOf(o)); >> >> Object.getOwnPropertyNames(o).forEach((name) => { >> Object.defineProperty(clone_of_o, name, >> Object.getOwnPropertyDescriptor(o) || {}); >> }); > > Which since ECMAScript 2017 reduces to > > var o = …; > > var clone_of_o = Object.create( > Object.getPrototypeOf(o), > Object.getOwnPropertyDescriptors(o)); > > :) > > It's still a shallow copy. I wrote time ago a unit tests related to cloning an object and now tried to use above code. The tests didn't pass. I mean, changing original object data mutates cloned object. :/ -- Cezary Tomczyk http://www.ctomczyk.pl/ https://www.aslint.org/ - Accessibility Validator