3
votes

When you cast null to an object using Object(null), the result is an object.

When you cast undefined to an object using Object(undefined), the result is an object.

But saying null as Object or undefined as Object remains null.

Why is this? I can't find anything in the documentation about this.

The objects that result from these casts can have properties set on and read from them. It's as if the cast created information, or removed whatever special information the null and undefined values have. But there's no indication in the AS3 documentation that the null and undefined special types are actually objects.

2
Simple answer? Because it's flash, and flash is bad. Writing a type with trailing parenthesis is declaring a new object and you're passing null as a constructor parameter for copy or a reinterpret type cast. It's essentially the same as saying var o = new Object(null);. You're still creating an object. - user562566
You're half right, @AscensionSystems. You are creating an Object, but it's not the same as calling new Object(), even though the result is the same. The most important thing to understand is that Object( something ) is not type casting at all: It is invoking the top level function called Object, which tries to return the Object value of something. And, as it turns out, the Object value of null is an empty Object. help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/… - weltraumpirat
You need to understand what null and undefined actually are. Null is when a variable has been declared but does not have any value. If a variable has not been declared then it is undefined and there will be a compiler error. trace(null == undefined); //Outputs true and trace(null === undefined); //Outputs false - The_asMan
Interesting nfo guys Ill check this out when I get home. Flash is a strange animal indeed. - user562566
weltraumpirat — Why is the Object value of null an empty Object? From the page you cited: "Every value in ActionScript 3.0 is an object, which means that calling Object() on a value returns that value." This seems to contradict, not reinforce, every explanation given so far in this discussion. - ivanreese

2 Answers

3
votes

There is a difference between the two casting techniques. Here is a good read on it: http://upshots.org/actionscript-3/as3-casting-objects

Basically, Object(null) actually converts null into an Object while "as" attempts to see if null can be treated as an Object, which it cannot.

3
votes

It's easier to explain with a String first.

Let's say we do this:

var v:* = null;
var s:String = v;

What happens? s becomes null. null is a valid value for references of type String.

Now instead:

var v:* = null;
var s:String = String(v);

What happens now? s becomes "null". We're explicitly asking for an object of type String here, but null is of type Null, so it must be converted. null converted to String is "null".

If you were to convert 123.45 to String, you would get "123.45". Fair enough.

Now let's try with Object.

var v:* = null;
var obj:Object = v;

obj becomes null.

Now:

var v:* = null;
var obj:Object = Object(v);

Here again we're explicitly asking for obj to point to a value of type Object - which null is not. null converted to Object is an empty object.

Let's see again:

var s:String = null;

Here the reference s of type String is pointing to the value null of type Null.

Reference String, value Null.

A String reference can take a Null value, but a Number reference cannot take a Null value, so when assigning null to a Number it must be converted (i.e. to 0).

var n:Number = Number(null); // 0

Thinking in terms of references and values really helps.

It also helps to remember that casting is for references, conversion is for values. Object(value) is a conversion. (value as Object) is a cast.

Finally, let's try with a user-defined type.

class Person {}

Then:

var person:Person = null;

Here person becomes null, as you would expect.

But:

var person:Person = Person(null);

person becomes null again! We asked for a Person, but we got a Null. How come?

The value null cannot be converted to type Person. In such a case, the result is the default value for the desired type. The default value for Person is null.

You can see this converting to Number as well.

var n:Number = Number("123.45");

n becomes 123.45.

var n:Number = Number("The quick brown fox, period.");

n becomes NaN.

As the string "The quick brown fox, period." cannot be converted to type Number, the result is the default value for Number, which is NaN. This is in contrast to the previous example where we successfully converted null to Number, yielding 0.

I could go on.

When in doubt, use the as operator. value as Type basically amounts to value is Type ? value : null. No complicated rules to remember. Use Type(value) only when you want to convert a value of one type to a value of another type.