Object Deletion AS2 vs AS3


#1

AS2:
var Object_Instance:Object = new Object();
Object_Instance.Name = “Bracer”;
Object_Instance.Age = 23;

trace (Object_Instance.Name); //Bracer
trace (Object_Instance.Age); //23

delete Object_Instance;

trace (Object_Instance.Name); //undefined
trace (Object_Instance.Age); //undefined

=====================================================================================
AS3:
var Object_Instance:Object = new Object();
Object_Instance.Name = “Bracer”;
Object_Instance.Age = 23;

trace (Object_Instance.Name); //Bracer
trace (Object_Instance.Age); //23

Object_Instance = null; //Since you can’t do delete Object_Instance; anymore…

trace (Object_Instance.Name); //IDE Error !
trace (Object_Instance.Age); //IDE Error !
TypeError: Error #1009: Cannot access a property or method of a null object reference.

Is there a way to get it to not throw an error but instead let it be professional and trace out undefined like in AS2 ?


#2

Not really. AS3 is more strict and throws errors more when you do things you shouldn’t be doing. AS2 was more lax and let you get away with more things ignoring that it was potentially a problem.


#3

Still…is there really no way of finding out if the object is no longer in existence without the entire virtual machine crashing like this ?
This is not “advanced”, this is a step backward in my opinion, like that time they removed duplicateMovieClip.


#4

You need to check if Object_Instance exists or not before you start pulling values from it. If you check Object_Instance.Name and get undefined in return, you don’t know if its Object_Instance.Name thats undefined or Object_Instance with AS2. And that can lead to bugs.


#5

And the very checking of it lead to the entire Virtual Machine crashing ?
That is deciding not cool.


#6

Its not crashing the virtual machine. It is resulting in an unhandled error which can have bad enough consequences that it exits the current call stack and produces a warning.

AS3 is simply more strict and AS2. This helps developers (even though it seems like a burden now), and helps maintain expectations in the runtime allowing it to execute code more quickly. You can still make projects with AS2 if you prefer how things work there. AS3 takes things up a notch, making you a better programmer by requiring that code be less sloppy which in turn also gives you a nice little speed boost.


#7

Thanks, next time, I will know to test for null :smiley: