Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #35340

Re: Recording changes to an object

Path csiph.com!.POSTED.208-71-239-109.cust.centrio.cz!not-for-mail
From Cezary Tomczyk <cezary.tomczyk@gmail.com>
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 <phdcst$1hro$1@csiph.com> (permalink)
References <aYCdnU3Tes0_oLbGnZ2dnUU7-LHNnZ2d@westnet.com.au> <1620852.VI2in3QvqR@PointedEars.de> <d9423a2c-3ca3-7571-6fa0-aca40b1c6d0f@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

Show key headers only | View raw


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));
> 
> :)
> 
> <http://ecma-international.org/ecma-262/8.0/#sec-object.getownpropertydescriptors>

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

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Recording changes to an object Andrew Poulos <ap_prog@hotmail.com> - 2018-06-21 15:54 +1000
  Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-21 01:47 -0700
  Re: Recording changes to an object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-06-21 17:01 +0100
    Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-21 09:48 -0700
      Re: Recording changes to an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-06-22 09:58 +0100
  Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-21 21:03 +0200
    Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-21 16:25 -0700
    Re: Recording changes to an object Andrew Poulos <ap_prog@hotmail.com> - 2018-06-22 09:34 +1000
      Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-21 17:02 -0700
        Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-21 18:03 -0700
        Re: Recording changes to an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-06-22 10:06 +0100
          Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-22 02:21 -0700
            Re: Recording changes to an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-06-22 18:31 +0100
              Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-22 20:16 -0700
      Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-22 02:23 +0200
        Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-21 17:31 -0700
      Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-22 02:55 +0200
        Re: Recording changes to an object Andrew Poulos <ap_prog@hotmail.com> - 2018-06-22 12:37 +1000
          Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-22 02:23 -0700
            Re: Recording changes to an object Jake Jarvis <pig_in_shoes@yahoo.com> - 2018-06-22 14:35 +0200
              Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-22 05:57 -0700
                Re: Recording changes to an object Mickey Finn <mfinn@gmail.com> - 2018-06-22 15:43 -0300
              Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-22 13:51 +0200
                Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-22 06:04 -0700
                Re: Recording changes to an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-06-22 18:33 +0100
                Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-24 14:36 +0200
                Re: Recording changes to an object "Mark-" <nospam@mapson.xyz> - 2018-06-24 13:56 +0000
                Re: Recording changes to an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-06-25 10:06 +0100
                Re: Recording changes to an object Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-06-25 02:40 -0700
                Re: Recording changes to an object Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-06-25 02:47 -0700
                Re: Recording changes to an object Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-06-25 02:51 -0700
                Re: Recording changes to an object Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-06-25 02:54 -0700
                Re: Recording changes to an object Jonas Thörnvall <jonas.thornvall@gmail.com> - 2018-06-25 02:58 -0700
                Re: Recording changes to an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-06-25 19:26 +0100
                Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-30 19:47 +0200
                Re: Recording changes to an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-07-01 10:56 +0100
            Re: Recording changes to an object Andrew Poulos <ap_prog@hotmail.com> - 2018-06-23 17:19 +1000
              Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-23 00:51 -0700
                Re: Recording changes to an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-06-23 11:27 +0100
                Re: Recording changes to an object Andrew Poulos <ap_prog@hotmail.com> - 2018-06-23 20:59 +1000
                Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-23 05:51 -0700
    Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-30 19:43 +0200
      Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-01 15:22 +0200
        Re: Recording changes to an object Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2018-07-02 16:29 +0200
          Cloning an object (was: Recording changes to an object) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-04 23:25 +0200
            Re: Cloning an object Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2018-07-05 10:00 +0200
              Re: Cloning an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-05 19:49 +0200
                Re: Cloning an object Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2018-07-05 20:37 +0200
                Re: Cloning an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-05 23:35 +0200
                Re: Cloning an object Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2018-07-05 23:54 +0200
                Re: Cloning an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-06 00:32 +0200
                Re: Cloning an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-06 00:24 +0200
                Re: Cloning an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-07-06 18:48 +0100
                Re: Cloning an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-06 23:51 +0200
                Re: Cloning an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-07-07 15:51 +0100
                Re: Cloning an object Julio Di Egidio <julio@diegidio.name> - 2018-07-07 08:39 -0700
                Re: Cloning an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-07-09 10:20 +0100
                Re: Cloning an object Julio Di Egidio <julio@diegidio.name> - 2018-07-09 02:35 -0700
                Re: Cloning an object Julio Di Egidio <julio@diegidio.name> - 2018-07-09 02:41 -0700
                Re: Cloning an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-08 12:20 +0200
                Re: Cloning an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-07-08 14:38 +0100
                Re: Cloning an object Julio Di Egidio <julio@diegidio.name> - 2018-07-08 13:10 -0700
                Re: Cloning an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-07-09 09:48 +0100
                Re: Cloning an object Julio Di Egidio <julio@diegidio.name> - 2018-07-09 02:28 -0700
                Re: Cloning an object Julio Di Egidio <julio@diegidio.name> - 2018-07-09 02:38 -0700
                Re: Cloning an object John G Harris <niam@jghnorth.org.uk.invalid> - 2018-07-09 19:11 +0100
      Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-07-02 21:01 -0700
        Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-05 00:32 +0200
          Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-07-04 19:23 -0700
            Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-05 06:38 +0200
              Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-07-04 23:21 -0700
                Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-05 09:25 +0200
                Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-07-05 07:44 -0700
                Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-05 19:35 +0200
                Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-07-06 12:59 -0700
                Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-06 23:22 +0200
                Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-07-07 12:45 -0700
                Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-08 11:13 +0200
                Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-07-08 05:03 -0700
                Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-07-08 13:42 -0700
                Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-07-08 13:46 -0700
                Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-07-08 13:41 -0700
                Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-09 00:35 +0200
                Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-07-10 01:03 -0700
                Re: Recording changes to an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-07-10 20:50 +0200
                Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-07-10 16:09 -0700
  Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-06-21 15:09 -0700
    Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-21 16:30 -0700
      Re: Recording changes to an object "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2018-06-21 16:57 -0700
        Re: Recording changes to an object Julio Di Egidio <julio@diegidio.name> - 2018-06-21 17:01 -0700

csiph-web