Path: csiph.com!aioe.org!news.mb-net.net!open-news-network.org!.POSTED.35.233.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Cloning an object Date: Fri, 06 Jul 2018 00:24:09 +0200 Organization: PointedEars Software (PES) Lines: 112 Message-ID: <4080533.LvFx2qVVIh@PointedEars.de> References: <1620852.VI2in3QvqR@PointedEars.de> <9909bfec-c6e7-2a4a-2a54-ec9f271691c8@PointedEars.de> <4395113.31r3eYUQgx@PointedEars.de> <506b67da-b086-3d0e-5985-d0f8214da432@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="35.233.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.233.35"; logging-data="16906"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.2 X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. 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: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.