Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #35260 > unrolled thread
| Started by | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| First post | 2018-06-21 15:54 +1000 |
| Last post | 2018-06-21 17:01 -0700 |
| Articles | 20 on this page of 90 — 11 participants |
Back to article view | Back to comp.lang.javascript
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
Page 3 of 5 — ← Prev page 1 2 [3] 4 5 Next page →
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2018-06-23 05:51 -0700 |
| Message-ID | <f6fc3566-c33c-4ae1-80ac-12ebd048b9fc@googlegroups.com> |
| In reply to | #35294 |
On Saturday, 23 June 2018 12:59:47 UTC+2, Andrew Poulos wrote: > On 23/06/2018 5:51 PM, Julio Di Egidio wrote: > > On Saturday, 23 June 2018 09:19:51 UTC+2, Andrew Poulos wrote: > >> On 22/06/2018 7:23 PM, Julio Di Egidio wrote: > >>> On Friday, 22 June 2018 04:38:00 UTC+2, Andrew Poulos wrote: > >>> > >>>> Thanks for the comprehensive replies. > >>> > >>> Thank you for not thanking me ever: now the gifts are over. Moron. > >> > >> I can't really comment on your last sentence but your first sentence is > >> untrue. > > > > No, it isn't, you moron: but you can try and prove me wrong. > > > > You never thank me and if it's happened once in the past by chance you can > > stick it up your ass: you are rather very careful to thank others, as can be > > seen in this thread, even and on purpose just after I have said how they are > > wrong. And you are certainly free to do so, but do save me the bullshit--- > > I'll save you as much of your own bs as you feel like eating. Sure, keep fighting the good fight... Moron. *Plonk* Julio
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2018-06-30 19:43 +0200 |
| Message-ID | <d9423a2c-3ca3-7571-6fa0-aca40b1c6d0f@PointedEars.de> |
| In reply to | #35264 |
Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
>> I started by making copies of the object each time it was changed but
>> within minutes there was a huge number of copies. I thought about
>> limiting the undo level to, say, 8 levels but if you set a property
>> value to the value it currently is (ie. you don't actually change the
>> value) it was still recording it as a change - so trying to do an undo
>> did "nothing".
>
> Yes, that is a terrible idea in the first place.
Upon further consideration, there is an advantage to that approach as it
will accurately preserve and restore the previous state(s) of the object.
Because property changes might not be reversible:
var universe = Object.create(null, (function () {
var _answer = 23;
return {
answer: {
"get": function () { return _answer; },
"set": function (newValue) {
if (newValue !== 42) return;
_answer = newValue;
}
}
};
}()));
universe.answer = 42;
/* You cannot restore the initial state because the setter prevents it */
universe.answer = 23;
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) || {});
});
There should be a *built-in* Object.clone() or Object.prototype.clone()
method that does this, but unfortunately there is not yet. So user
libraries have to come to the rescue.
PointedEars
--
When all you know is jQuery, every problem looks $(olvable).
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2018-07-01 15:22 +0200 |
| Message-ID | <9909bfec-c6e7-2a4a-2a54-ec9f271691c8@PointedEars.de> |
| In reply to | #35329 |
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>
> There should be a *built-in* Object.clone() or Object.prototype.clone()
> method that does this, but unfortunately there is not yet. So user
> libraries have to come to the rescue.
This has not changed with ES 2017, though, and is not going to change with
ES 2018 as of today:
<http://ecma-international.org/ecma-262/9.0/>
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2018-07-02 16:29 +0200 |
| Message-ID | <phdcst$1hro$1@csiph.com> |
| In reply to | #35338 |
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
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2018-07-04 23:25 +0200 |
| Subject | Cloning an object (was: Recording changes to an object) |
| Message-ID | <4395113.31r3eYUQgx@PointedEars.de> |
| In reply to | #35340 |
Cezary Tomczyk wrote:
> 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. :/
Deep copies are easy with recursion. In this case, tested in
navigator.userAgent === "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36":
function clone (obj, deepCopy)
{
const CLONE = Object.create(Object.getPrototypeOf(obj));
Object.getOwnPropertyNames(obj).forEach((name) => {
const DESC = Object.getOwnPropertyDescriptor(obj, name);
if (deepCopy)
{
/* FIXME: Standardize Object.isObject() */
if (("value" in DESC)
&& (typeof DESC.value == "object")
&& (DESC.value != null))
{
DESC.value = clone(DESC.value);
}
}
Object.defineProperty(CLONE, name, DESC || {});
});
return CLONE;
}
const ORIG = {foo: {bar: "baz"}};
const CLONE = clone(ORIG, true);
/* "baz" */
console.log(CLONE.foo.bar);
CLONE.foo.bar = 42;
/* 42 */
console.log(CLONE.foo.bar);
/* "baz" */
console.log(ORIG.foo.bar);
See also jsx.object.clone() in JSX:object.js (not yet adapted to ES 6, but
with more options):
<https://github.com/PointedEars/JSX/blob/master/object.js#L601-L695>
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2018-07-05 10:00 +0200 |
| Subject | Re: Cloning an object |
| Message-ID | <phkj7m$2ugj$1@csiph.com> |
| In reply to | #35343 |
On 04/07/2018 23:25, Thomas 'PointedEars' Lahn wrote:
[...]
> Deep copies are easy with recursion. In this case, tested in
> navigator.userAgent === "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36
> (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36":
>
> function clone (obj, deepCopy)
> {
> const CLONE = Object.create(Object.getPrototypeOf(obj));
>
> Object.getOwnPropertyNames(obj).forEach((name) => {
> const DESC = Object.getOwnPropertyDescriptor(obj, name);
>
> if (deepCopy)
> {
> /* FIXME: Standardize Object.isObject() */
> if (("value" in DESC)
> && (typeof DESC.value == "object")
> && (DESC.value != null))
> {
> DESC.value = clone(DESC.value);
> }
> }
>
> Object.defineProperty(CLONE, name, DESC || {});
> });
>
> return CLONE;
> }
>
> const ORIG = {foo: {bar: "baz"}};
> const CLONE = clone(ORIG, true);
>
> /* "baz" */
> console.log(CLONE.foo.bar);
>
> CLONE.foo.bar = 42;
>
> /* 42 */
> console.log(CLONE.foo.bar);
>
> /* "baz" */
> console.log(ORIG.foo.bar);
>
> See also jsx.object.clone() in JSX:object.js (not yet adapted to ES 6, but
> with more options):
>
> <https://github.com/PointedEars/JSX/blob/master/object.js#L601-L695>
It passes for simple objects, but if the property has a value as an
array or Date object then it doesn't work. But I think above solution
can be enhanced to support it.
--
Cezary Tomczyk
http://www.ctomczyk.pl/
https://www.aslint.org/
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2018-07-05 19:49 +0200 |
| Subject | Re: Cloning an object |
| Message-ID | <506b67da-b086-3d0e-5985-d0f8214da432@PointedEars.de> |
| In reply to | #35349 |
Cezary Tomczyk wrote:
> On 04/07/2018 23:25, Thomas 'PointedEars' Lahn wrote:
>> function clone (obj, deepCopy)
>> {
>> const CLONE = Object.create(Object.getPrototypeOf(obj));
>>
>> Object.getOwnPropertyNames(obj).forEach((name) => {
>> const DESC = Object.getOwnPropertyDescriptor(obj, name);
>>
>> if (deepCopy)
>> {
>> /* FIXME: Standardize Object.isObject() */
>> if (("value" in DESC)
>> && (typeof DESC.value == "object")
>> && (DESC.value != null))
>> {
>> DESC.value = clone(DESC.value);
>> }
>> }
>>
>> Object.defineProperty(CLONE, name, DESC || {});
>> });
>>
>> return CLONE;
>> }
>> […]
>
> 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?
> But I think above solution can be enhanced to support it.
Yes, it should.
.
.
.
.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2018-07-05 20:37 +0200 |
| Subject | Re: Cloning an object |
| Message-ID | <phloi4$kb2$1@csiph.com> |
| In reply to | #35352 |
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)
>>> {
>>> const CLONE = Object.create(Object.getPrototypeOf(obj));
>>>
>>> Object.getOwnPropertyNames(obj).forEach((name) => {
>>> const DESC = Object.getOwnPropertyDescriptor(obj, name);
>>>
>>> if (deepCopy)
>>> {
>>> /* FIXME: Standardize Object.isObject() */
>>> if (("value" in DESC)
>>> && (typeof DESC.value == "object")
>>> && (DESC.value != null))
>>> {
>>> DESC.value = clone(DESC.value);
>>> }
>>> }
>>>
>>> Object.defineProperty(CLONE, name, DESC || {});
>>> });
>>>
>>> return CLONE;
>>> }
>>> […]
>>
>> 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 o = {
a: 1,
b: [
{
test: 'test'
}
]
};
var cloned = clone(o);
o.b[0].test = 'something';
expect(cloned.b[0].test).toBe('test');
This test doesn't pass. Basically, modifications on original object
should not modify cloned object.
--
Cezary Tomczyk
http://www.ctomczyk.pl/
https://www.aslint.org/
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2018-07-05 23:35 +0200 |
| Subject | Re: Cloning an object |
| Message-ID | <5399415.lOV4Wx5bFT@PointedEars.de> |
| In reply to | #35353 |
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) :)
.
.
.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Date | 2018-07-05 23:54 +0200 |
| Subject | Re: Cloning an object |
| Message-ID | <phm42o$rk5$1@csiph.com> |
| In reply to | #35354 |
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/
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2018-07-06 00:32 +0200 |
| Subject | Re: Cloning an object |
| Message-ID | <2080670.ElGaqSPkdT@PointedEars.de> |
| In reply to | #35355 |
Cezary Tomczyk wrote: > On 05/07/2018 23:35, Thomas 'PointedEars' Lahn wrote: >> 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. That *your* code, that *you* *think* is implementing my approach, which you do not show, is not working as you expect, does not invalidate *my* approach and *my* code that I wrote to implement it. Even though my originally posted code has that bug that *I* have found as you forced me to look at it again: When *actually* called with deepCopy === true, it only copies *one* level deep. > 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. Look more carefully at the code again, read my follow-up again, and do read my other follow-up; you appear to have the common misconception of "nested objects" in ES. HTH -- PointedEars FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix> <https://github.com/PointedEars> | <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2018-07-06 00:24 +0200 |
| Subject | Re: Cloning an object |
| Message-ID | <4080533.LvFx2qVVIh@PointedEars.de> |
| In reply to | #35353 |
Cezary Tomczyk wrote:
> var o = {
> a: 1,
> b: [
> {
> test: 'test'
> }
> ]
> };
>
> var cloned = clone(o);
>
> o.b[0].test = 'something';
>
> expect(cloned.b[0].test).toBe('test');
>
> This test doesn't pass. Basically, modifications on original object
> should not modify cloned object.
The object referred to by “o.b[0]” is NOT part of the original object,
therefore if you do _not_ make a “deep copy”, then only the *reference
value* stored in the “b” property of the original object is cloned (copied).
Despite the syntax that suggests otherwise, ECMAScript objects are NOT like
that:
,---------------------------------.
: o : Object :
:---------------------------------:
: +a : number = 1 :
: ,-----------------------------. :
: : +b : Array : :
: : ,-------------------------. : :
: : : +0 : Object : : :
: : :-------------------------: : :
: : : +test : string = "test" : : :
: : `-------------------------' : :
: `-----------------------------' :
`---------------------------------'
But like this:
,-----------------.
o --> : : Object :
:--------------:--:
: +a : number = 1 : ,--------------.
: +b : Array -------> : : Array :
`-----------------' :--------------: ,-------------------------.
: +0 : Object ---> : : Object :
`--------------' :-------------------------:
: +test : string = "test" :
`-------------------------'
When you clone an object without making a “deep copy”, i.e. a *clone* of the
*objects* that the first object’s properties *refer to*, then the clone will
access the same objects:
,-----------------.
o --> : : Object :
:--------------:--:
: +a : number = 1 : ,------------.-.
: +b : Array -------> : : Array :
`-----------------' :--------------: ,-------------------------.
: +0 : Object ---> : : Object :
`--------------' :-------------------------:
^ : +test : string = "test" :
: `-------------------------'
:
,-----------------. :
cloned --> : : Object : :
:--------------:--: :
: +a : number = 1 : :
: +b : Array -----------'
`-----------------'
vs. (“deep copy”)
,-----------------.
o --> : : Object :
:--------------:--:
: +a : number = 1 : ,------------.-.
: +b : Array -------> : : Array :
`-----------------' :--------------: ,-------------------------.
: +0 : Object ---> : : Object :
`--------------' :-------------------------:
: +test : string = "test" :
`-------------------------'
,-----------------.
cloned --> : : Object :
:--------------:--:
: +a : number = 1 : ,------------.-.
: +b : Array -------> : : Array :
`-----------------' :--------------:
: +0 : Object -----.
`--------------' :
:
,-------------------------. :
: : Object :<-'
:-------------------------:
: +test : string = "test" :
`-------------------------'
This important difference is why I insist on saying “the object referred to
by” instead of just “object”, and only using shorthand terminology with
previous definition.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2018-07-06 18:48 +0100 |
| Subject | Re: Cloning an object |
| Message-ID | <umavjd5796m2hufhbs2lo64501ic2lvrul@4ax.com> |
| In reply to | #35356 |
On Fri, 06 Jul 2018 00:24:09 +0200, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote: <snip> >Despite the syntax that suggests otherwise, ECMAScript objects are NOT like >that: > > ,---------------------------------. > : o : Object : > :---------------------------------: > : +a : number = 1 : > : ,-----------------------------. : > : : +b : Array : : > : : ,-------------------------. : : > : : : +0 : Object : : : > : : :-------------------------: : : > : : : +test : string = "test" : : : > : : `-------------------------' : : > : `-----------------------------' : > `---------------------------------' > >But like this: > > ,-----------------. > o --> : : Object : > :--------------:--: > : +a : number = 1 : ,--------------. > : +b : Array -------> : : Array : > `-----------------' :--------------: ,-------------------------. > : +0 : Object ---> : : Object : > `--------------' :-------------------------: > : +test : string = "test" : > `-------------------------' <snip> There is a bijection between the two types of diagram [*]. They say the same thing. Which one is preferred is a matter of personal choice, sometimes governed by the question to be answered. For instance, the version with arrows makes it more obvious that deep clone must beware of objects that appear more than once in the diagram. John [*] As it happens, this is proved in my thesis but the maths is not for the faint-hearted.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2018-07-06 23:51 +0200 |
| Subject | Re: Cloning an object |
| Message-ID | <bab80a83-c9f7-eeeb-63e0-e0f221802d5f@PointedEars.de> |
| In reply to | #35360 |
John G Harris wrote: > On Fri, 06 Jul 2018 00:24:09 +0200, Thomas 'PointedEars' Lahn > <PointedEars@web.de> wrote: > > <snip> >> Despite the syntax that suggests otherwise, ECMAScript objects are NOT like >> that: >> >> ,---------------------------------. >> : o : Object : >> :---------------------------------: >> : +a : number = 1 : >> : ,-----------------------------. : >> : : +b : Array : : >> : : ,-------------------------. : : >> : : : +0 : Object : : : >> : : :-------------------------: : : >> : : : +test : string = "test" : : : >> : : `-------------------------' : : >> : `-----------------------------' : >> `---------------------------------' >> >> But like this: >> >> ,-----------------. >> o --> : : Object : >> :--------------:--: >> : +a : number = 1 : ,--------------. >> : +b : Array -------> : : Array : >> `-----------------' :--------------: ,-------------------------. >> : +0 : Object ---> : : Object : >> `--------------' :-------------------------: >> : +test : string = "test" : >> `-------------------------' > <snip> > > There is a bijection between the two types of diagram [*]. No, there is _not_. > They say the same thing. No, they do _not_. As you can see in the diagrams that you did not quote. The first kind of object *contains* other objects. For example, a box of chocolates *contains* chocolate bars. The association between objects there is COMPOSITION: an object is *made up* of other objects. Cloning the first kind of object implicitly clones the objects it *contains*. If you eat a certain number of chocolate bars in the original box, and had some method to clone it afterwards, for example advanced teleportation where the original is not destroyed, the clone would contain as many chocolate bars, arranged in the same way, as does the original. The second kind of object only *refers to* other objects. For example, your thesis, if it otherwise is any good, refers to other scientific works by citation. Those works are _not_ *contained* in your thesis. Therefore, other people’s theses can refer to the same works or a subset of them. Another example would be relationships of people; in those, no person is contained in the other, and your sibling is a child of your parents as well as you are. The association there is REFERENCE. By contrast to composition, this is a purely *abstract* form of association between objects. Cloning the second kind of object does _not_ *implicitly* clone the referred objects. That is why, if you want the second kind of object to provide data that are *independent* of the data that the original object provides, through all levels, you have to perform a “*deep* copy/clone”: You have to clone the *referred* objects and *update* the reference in the referring clone to refer to the other clones. > [*] As it happens, this is proved in my thesis but the maths is not > for the faint-hearted. If your thesis makes that claim, then, I am sorry to break it to you, your thesis is *wrong*. And one really does not need advanced maths to see that. Keep in mind that the way object orientation is implemented in the programming languages that you may know (which obviously do not include ECMAScript and its ECMAScript implementations) are _not_ the only way in which object orientation can be implemented. PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2018-07-07 15:51 +0100 |
| Subject | Re: Cloning an object |
| Message-ID | <1qk1kdhp07b01qu94k1t3caporpplbfej7@4ax.com> |
| In reply to | #35363 |
On Fri, 6 Jul 2018 23:51:01 +0200, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote: As I expected, Thomas has reacted in his usual way, using his favourite debating technique. <snip> >For example, your >thesis, if it otherwise is any good, <snip> Good enough for the external examiner to say it had passed. >If your thesis makes that claim, then, I am sorry to break it to you, your >thesis is *wrong*. And one really does not need advanced maths to see that. <snip> A professional mathematician, one of the internal examiners, proof-read the maths in detail before the thesis was submitted. John -- Small fact : The second sentence in section 11 of the Standard is the only reason why newThing() cannot be parsed the same as new Thing().
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2018-07-07 08:39 -0700 |
| Subject | Re: Cloning an object |
| Message-ID | <9570c118-9dee-4ba9-bb8e-65fc6a944248@googlegroups.com> |
| In reply to | #35367 |
On Saturday, 7 July 2018 16:51:53 UTC+2, John G Harris wrote: > On Fri, 6 Jul 2018 23:51:01 +0200, Thomas 'PointedEars' Lahn > <PointedEars@web.de> wrote: <snip> > > If your thesis makes that claim, then, I am sorry to break it to you, your > > thesis is *wrong*. And one really does not need advanced maths to see that. > <snip> > > A professional mathematician, one of the internal examiners, > proof-read the maths in detail before the thesis was submitted. FWIW, I agree with Thomas here: the two diagrams are equivalent just "diagrammatically", i.e. they are equivalent by disregarding the technical information conveyed by the second over the first, i.e. that an object contains just references to other objects, which is about the actual underlying data-structures. Indeed, *as technical diagrams*, the first one is simply incorrect, at best a simplification of the second because you didn't have room on the page or something... (And, sorry to break it on you ;), but Programming is not mathematics, it's an engineering (utterly different principles, discipline and goals): so, beware of that code, you have proved it correct, but that's just not the actual story...) Julio
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2018-07-09 10:20 +0100 |
| Subject | Re: Cloning an object |
| Message-ID | <j4a6kd50ht74kco7t5o5th7p6j4n48n42p@4ax.com> |
| In reply to | #35369 |
On Sat, 7 Jul 2018 08:39:39 -0700 (PDT), Julio Di Egidio <julio@diegidio.name> wrote: >On Saturday, 7 July 2018 16:51:53 UTC+2, John G Harris wrote: >> On Fri, 6 Jul 2018 23:51:01 +0200, Thomas 'PointedEars' Lahn >> <PointedEars@web.de> wrote: ><snip> >> > If your thesis makes that claim, then, I am sorry to break it to you, your >> > thesis is *wrong*. And one really does not need advanced maths to see that. >> <snip> >> >> A professional mathematician, one of the internal examiners, >> proof-read the maths in detail before the thesis was submitted. > >FWIW, I agree with Thomas here: the two diagrams are equivalent just >"diagrammatically", I said the two diagrams have the same structure, which is what you have now said and what Thomas denied. >i.e. they are equivalent by disregarding the technical >information conveyed by the second over the first, i.e. The first diagram has exactly same text as the second, so how on earth can it be disregarding technical information? >that an object >contains just references to other objects, which is about the actual >underlying data-structures. In your favourite implementation yes. In *all* conforming implementations : you haven't even attempted to produce a convincing argument yet. >Indeed, *as technical diagrams*, the first >one is simply incorrect, at best a simplification of the second because >you didn't have room on the page or something... As above, there's a distinct lack of simplification. >(And, sorry to break it on you ;), but Programming is not mathematics, it's an >engineering (utterly different principles, discipline and goals): Electrical Engineers use mathematics. Mechanical Engineers use mathematics. Software Engineers ... Well, the name was invented in the hope that software people would become less sloppy. Several thousand angry bank customers (look up TSB failure) will tell you it hasn't happened yet, unfortunately. >so, beware of >that code, you have proved it correct, but that's just not the actual story...) You don't prove equivalence of structures by writing code. "It compiles. Ship it!" is not even the beginnings of a proof. John
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2018-07-09 02:35 -0700 |
| Subject | Re: Cloning an object |
| Message-ID | <50b9bc97-af34-4b6f-bb7f-debfdcdafe0f@googlegroups.com> |
| In reply to | #35408 |
On Monday, 9 July 2018 11:20:30 UTC+2, John G Harris wrote: > Electrical Engineers use mathematics. Mechanical Engineers use > mathematics. Software Engineers ... Well, the name was invented in the > hope that software people would become less sloppy. You are not even decent enough to kiss my ass, you hopeless retarded fraudulent ignorant piece of spamming shit. > Several thousand > angry bank customers (look up TSB failure) will tell you it hasn't > happened yet, unfortunately. Sure, TDD among your *utter stupidity* and then the frauds. Bunch of shameless retarded fraudulent world-polluting cunts... *Plonk* Julio
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2018-07-09 02:41 -0700 |
| Subject | Re: Cloning an object |
| Message-ID | <9e9c428b-8610-412a-8d74-8104d05683d2@googlegroups.com> |
| In reply to | #35408 |
On Monday, 9 July 2018 11:20:30 UTC+2, John G Harris wrote: > On Sat, 7 Jul 2018 08:39:39 -0700 (PDT), Julio Di Egidio <snip> > > FWIW, I agree with Thomas here: the two diagrams are equivalent just > > "diagrammatically", > > I said the two diagrams have the same structure, which is what you > have now said and what Thomas denied. My mistake mentioning that fucking idiot: he is as anal on specs and stupidities as he doesn't really know what software even is and what he is talking about. Another missed pseudo-mathematician... EOD for now, stupid fucks. *Plonk* Julio
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2018-07-08 12:20 +0200 |
| Subject | Re: Cloning an object |
| Message-ID | <0f9144fe-2a84-3737-08e5-1c28695c9d49@PointedEars.de> |
| In reply to | #35367 |
John G Harris wrote: > On Fri, 6 Jul 2018 23:51:01 +0200, Thomas 'PointedEars' Lahn > <PointedEars@web.de> wrote: > > As I expected, Thomas has reacted in his usual way, using his > favourite debating technique. Fallacy: Poisoning the well. >> If your thesis makes that claim, then, I am sorry to break it to you, your >> thesis is *wrong*. And one really does not need advanced maths to see that. > <snip> > > A professional mathematician, one of the internal examiners, > proof-read the maths in detail before the thesis was submitted. Regardless whether this is true, the diagrams that you did not quote, and the outcome of the application of the source code (as shown in the discussion with Cezary), prove you wrong: If an object contains other objects, then its shallow clone will contain objects that have the same structure and the same values as the objects contained in the original. Modifying one of the contained objects in the original will not modify the corresponding object in the clone, and vice-versa. If an object contains only references to other objects instead of the objects themselves, then its shallow clone will only contain references to the same objects as the original. Modifying one of the referred objects will modify the values of the properties it provides in both the original and the clone. If I am not mistaken, the mathematical formulation is that there exists an isomorphism on objects, shallow cloning, under which the two object structures are not isomorphic. PointedEars -- When all you know is jQuery, every problem looks $(olvable).
[toc] | [prev] | [next] | [standalone]
Page 3 of 5 — ← Prev page 1 2 [3] 4 5 Next page →
Back to top | Article view | comp.lang.javascript
csiph-web