On 12/3/05, Boon Chew <[EMAIL PROTECTED]> wrote:
> I am trying to get a class to destroy itself, calling delete obj looks too
> unnatural, amidst all OO code.
>
> Could someone explain why the following doesn't work?
> The following code doesn't work, is there another way to achieve this?
> Also,
>
> class A
> {
> function destroy()
> {
> delete this; // doesn't delete itself
> }
> }
>
> var a = new A();
> delete a; // delete works here
'delete' only removes the reference, not the actual object. There is
no way to actually destroy an object. All you can do is remove all
references to it, and wait for the GC to remove it from memory at some
unspecified time.
var obj = {data:5};
var obj_ref = obj;
obj_ref.data = 6;
// this proves that it's a reference, and not a copy: both show the same
// value, even though I only changed one
trace(obj.data); // 6
trace(obj_ref.data); // 6
// delete the reference
delete obj;
// as you can see, the object itself was not deleted, only that
// particular reference.
trace(obj.data); // undefined
trace(obj_ref.data); // 6
-David R
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders