Thank you for the advice Aaron.
So:
var clonedHash = new Hash(toBeClonedHash);
And that's whats done internally:
function(object){
if ($type(object) == 'hash') object = $unlink(object.getClean());
for (var key in object) this[key] = object[key];
return this;
}
This is the standard way of cloning a hash.
getClean() will transform a Hash object into a normal javascript Object.
Then it goes to the $unlink function that will recursively copy all the
primitive types to 'this', which is a Hash object.
Hope its clear now.
--
Fábio Miranda Costa
Solucione Sistemas
Engenheiro de interfaces
On Wed, Jan 13, 2010 at 3:22 PM, אריה גלזר <[email protected]> wrote:
> ok. so i started digging in the core's code to understand better how to do
> this best. I noted the $unlink, when passed a hash, simply does
> return new Hash(object)
> this seemed odd, because i couldn't understand how it solves the recrusive
> issue, so i figured i'll find the answer at the Hash constructor, so i went
> to the Hash code base. which apparently, has no constructor (at least not
> here<http://github.com/mootools/mootools-core/blob/master/Source/Native/Hash.js>).
> so i decided to find getClean to see if it solves the recursive issue. but
> again - no getClean method
> there<http://github.com/mootools/mootools-core/blob/master/Source/Native/Hash.js>.
>
> so i was thinking this probably means Hash gets its methods and constructor
> from other natives, but couldn't find which.
>
> so a few questions -
> 1. where exactly does Hash gets its constructor from and what kind of
> checks does it do (if any) to make sure it really does create a truly new
> object (and if none is the answer, the $unlink code is broken).
> 2. where does getClean comes from? is it recursive?
> 3. will $unlink really do the trick for me (assuming it will actually
> work)? or do i need to do $unlink(hash.getClean()) for it to function
> properly?
>
> cloning an object or an array seems like a trivial operation to me. It
> seems odd that this is both so complex and also that moo doesn't have a
> fully supported way of doing this (or at least a coding standard for it).
> *
> (btw - this isn't criticism - if that's what it sounds like - i really dig
> moo and you developers really take programing with JS to a completely new
> level. I'm just curious about it). *
> -----------
> אריה גלזר
> 052-5348-561
> 5561
>
>
> On Wed, Jan 13, 2010 at 18:25, Aaron Newton <[email protected]> wrote:
>
>> $unlink isn't documented; the risk being that it may not be supported in
>> the future.
>>
>> 2010/1/13 Fábio M. Costa <[email protected]>
>>
>> var newHash = $unlink(oldHash);
>>>
>>>
>>> --
>>> Fábio Miranda Costa
>>> Solucione Sistemas
>>> Engenheiro de interfaces
>>>
>>>
>>>
>>> On Tue, Jan 12, 2010 at 11:25 PM, Eneko Alonso
>>> <[email protected]>wrote:
>>>
>>>> It's simple... just create a new Hash.
>>>>
>>>> var clon = new Hash(clonedHash.getClean());
>>>>
>>>>
>>>> That should work
>>>>
>>>>
>>>>
>>>> On Sat, Jan 9, 2010 at 11:54 AM, אריה גלזר <[email protected]>wrote:
>>>>
>>>>> reading more i saw that this supplies me with a non-hash copy, but i do
>>>>> need a hash...
>>>>>
>>>>> -----------
>>>>> אריה גלזר
>>>>> 052-5348-561
>>>>> 5561
>>>>>
>>>>>
>>>>> On Sat, Jan 9, 2010 at 21:51, אריה גלזר <[email protected]>wrote:
>>>>>
>>>>>> im looking to clone the hash, not create an empty one...
>>>>>>
>>>>>> -----------
>>>>>> אריה גלזר
>>>>>> 052-5348-561
>>>>>> 5561
>>>>>>
>>>>>>
>>>>>> On Sat, Jan 9, 2010 at 21:27, Piotr Zalewa <[email protected]> wrote:
>>>>>>
>>>>>>> a = this.hash.getClean()
>>>>>>> will give you a standard object from a Hash
>>>>>>>
>>>>>>> 2010/1/9 אריה גלזר <[email protected]>
>>>>>>>
>>>>>>> hey. this is a very simple question that i'm dealing with -
>>>>>>>> i have a class with a hash member. i want to send a copy of that
>>>>>>>> hash to another function without modifying the original.
>>>>>>>> doing:
>>>>>>>>
>>>>>>>> var a = this.hash
>>>>>>>>
>>>>>>>> will only create a reference. is there a simple way of copying
>>>>>>>> objects or should i just iterate over it to create a new one?
>>>>>>>> -----------
>>>>>>>> אריה גלזר
>>>>>>>> 052-5348-561
>>>>>>>> 5561
>>>>>>>> Nothing selected
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> --
>>>>>>> London, UK
>>>>>>>
>>>>>>
>>>>>> Nothing selected
>>>>>>
>>>>>
>>>>> Nothing selected
>>>>>
>>>>
>>>>
>>>
>>
>