Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Object.clone() doesn't create a copy, keeps references

  • Please log in to reply
5 replies to this topic
  • Guests
  • Last active:
  • Joined: --
Hi guys,
today I found an interesting thing, which might be a bug.

See this code:
test := Object(), test.one := Object()
test2 := test.clone()

test := Object(), test.one := Object()
test2 := Object(), test2.one := Object()

test := Object(), test.one := Object()
test2 := copyObject(test)


output() {
	test.a := "empty", test.one.a := "a"
	MsgBox % "1: " . test.one.a . "`n2: " . test2.one.a	. "`n`n" . "1: " . test.a . "`n2: " . test2.a

copyObject(input) {
	output := Object()
	for key, val in input
		output[key] := IsObject(val) ? copyObject(val) : val
	return output	
Why is there a difference between cloning an Object that contains other (empty) objects and recreating the object myself? In my opinion, the Object.clone() function should clone the Object with all it's subobjects as it is at the moment and don't just keep the references of the subobjects.

I created a little function for myself that really copies an object and does not keep the references.
Can you tell me, if the way it is now is the way it is meant to be? If so, could you tell me why? I can't think of a reason for this.

Regards, nfl

[Moved from Bug Reports. ~jaco0646]

  • Moderators
  • 3165 posts
  • Last active: Apr 01 2014 01:46 AM
  • Joined: 07 Oct 2006
You may find an answer in here: Possible documentation error in "Object Methods".

  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
	nobj := obj.Clone()
	for k,v in nobj
		if IsObject(v)
			nobj[k] := A_ThisFunc.(v)
	return nobj


  • Moderators
  • 717 posts
  • Last active: Jul 31 2014 08:27 PM
  • Joined: 27 Mar 2008
Hmm, the clone() method doesn't take any args.

Lexikos: perhaps you could do this: obj.clone(True) which does a deep copy of obj (excluding base?) so both methods are available.

  • Guests
  • Last active:
  • Joined: --
Thanks jaco0646, now I understand it's much clearer.

Damn, fincs, yours seem to be slightly faster :D

infogulch, that's a nice idea, I would like to have it built in.

  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
fincs function can be further optimized by calling the function directly (ObjFullyClone(v)) instead of dynamically (A_ThisFunc.(v)) and calling ObjClone(obj) instead of obj.Clone().

It has a fatal flaw: circular references crash the script.