While checking out the Socket class i found a cool little trick. I´m sure everyone knows of the possibility to clone an Object with the help of the ByteArray Class. This method was introduced (at least to me ;-)) by senocular here.
The function looks like this:
function clone(source:Object):* {
var copier:ByteArray = new ByteArray();
copier.writeObject(source);
copier.position = 0;
return(copier.readObject());
}
The problem was that all the class information was lost. With class information i mean type info and thinks like methods were unavailable after cloning the object. When you try this:
var t:Test=new Test();
t.test=”hallo”;
var newObj=clone(t);
out.text=newObj.test;
out.text=newObj.sayHello();
I get the following error:
TypeError: Error #1006: sayHello ist keine Funktion.
at test/::show()
at test/___Button2_click()
But there is this nice Method registerClassAlias from the flash.net package. Using this:
registerClassAlias(“de.benz.something”, Test);
var t:Test=new Test();
t.test=”hallo”;
var newObj=clone(t);
out.text=newObj.test;
out.text=newObj.sayHello();
will keep the class information intact and the call to the method will be succesfull.
Man this is sooo cool. I´m loocking forward using this in two years when my company switches to flash 9 🙂
Anyone looking for a flex developer?
cheers
Benz
I