author: klitze7-c004 [+], Submitted: 06.07.02 10a • Last Edit: 06.07.02 10a
Avg. Rating: 4

usage

msg1 { drzoode [+], posted: 09.29.02 11p•-, top [^] }
..very nice subtle recursive function.
I have tested and found that it works with custom classes too
Only thing, I couldn't guess why my sample code here doesn't work:


msg2 { senocular [+], posted: 09.30.02 5p•03.10.03 4p, top [^] }
You're using MX, that proto works for Flash 5. In MX (which I think was actually meant to work in 5 but didnt) if you pass an object into the constructor of a new one (ie new Object(myObj) as with what was being done when passing this to the constructor in that proto) then its equivalent to saying newObj = myObj, which only make an object reference and not a complete copy - hense the reasoning for this prototype to begin with, to prevent that. The reason klitze7-c004 passed a this into the constructor was for non object objects like numbers. Then the new number would not just equal a new number but a new number with a value of that actual number. Of course now, with MX, we want that but only with the numbers or else object object creation will make references to the original object.

Heres a quick workaround (though very limited):

Object.prototype.copy = function(){
var _t = new this.__constructor__();
var count = 0;
for(i in this){
count++;
_t[i] = this[i].copy();
}
if (!count) _t = new this.constructor(this);
return _t;
};
ASSetPropFlags(Object.prototype,["copy"],1);

this only passes in the this into the constructor only if there are any iterations through the created object using the for..in. Normal numbers will have nothing in them other than their own value so count will be 0 where then the this is passed into the constructor.

I say this is limited because if you make a number prototype for example, the for..in will pick up on that going through the for..in and count will be more than 0 - here you can have an infinite loop recursion. ASSetPropFlags can prevent the prototype from being seen, but it would require you to set that for all of your prototypes.
msg3 { drzoode [+], posted: 09.30.02 10p•10.01.02 1a, top [^] }
Hi senocular,

Thank you for the detailed explanation, I appreciate that.
As far as I understand, in the above code, we have passed the composite data as a reference to the function, as they have used the same memory slot, a change in one instance affected the other.
As you have mentioned, we can do without __proto__ there.
Both this.constructor() and the *undocumented* this__constructor__() works.
It seems like we can also hide the properties of the cloned object in our method.
Here is what I think:





and a simple test:





..needs more testing
msg4 { klitze7-c004 [+], posted: 01.29.03 1a•-, top [^] }
this should work it's all about not copying a scalar value through the copy method but through assigning




example.


msg5 { doug [+], posted: 05.07.03 8a•-, top [^] }
This works great. Only thing - make sure to move 'ASSetPropFlags(Object.prototype,["copy"],1)'
to outside of the MX prototype function. I ran into a for in loop issue. Outside of that - works like a charm. Thanks - doug