Why is casting Object (null) not null?

When you pass null to an object with Object(null)

, the result is an object.

When you apply undefined to an object with Object(undefined)

, the result is an object.

But speaking null as Object

or undefined as Object

stays null.

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

Objects that result from these casts can have properties set and read from them. It is as if the information was cast or any special information that matters null

and was removed undefined

. But in AS3 documentation there is no indication that the special types null

and undefined

are actually objects.

+3


source to share


2 answers


There is a difference between the two casting methods. Here is some good text: http://upshots.org/actionscript-3/as3-casting-objects



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

+3


source


Easier to explain at first String

.

Let's say we do this:

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

      

What's happening? s

becomes null

. null

is a valid value for type references String

.

Now instead of this:

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

      

What's going on now? s

becomes "null"

. We are explicitly requesting a type object String

here, but it null

has a type null

, so it must be converted. null

converted to String

is "null"

.

If you needed to convert 123.45

to String

, you get "123.45"

. Fair enough.

Now 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 are asking for a obj

value of a type Object

- which is null

not. null

converted to Object

is an empty object.

Let's see again:

var s:String = null;

      

Here the s

type reference String

points to the value of the null

type null

.

Link String

, value null

.

A String

reference can take on a value null

, but a reference Number

cannot take on a value null

, so when assigning to null

a, Number

it must be converted (i.e. to 0

).

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

      



Thinking in terms of references and meanings really helps.

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

is a transformation. (value as Object)

is cast.

Finally, try with a custom type.

class Person {}

      

Then:

var person:Person = null;

      

Here person

it becomes null

as you would expect.

But:

var person:Person = Person(null);

      

person

becomes again null

! We asked person

, but we got it null

. Why?

The value null

cannot be converted to type person

. In this case, the result is the default for the desired type. The default value for person

is null

.

You can also see this conversion in Number

.

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

      

n

becomes 123.45

.

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

      

n

becomes NaN

.

Since 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 differs from the previous example where we successfully converted null

to Number

, getting 0

.

I could go on.

Use operator when in doubt as

. value as Type

basically makes up value is Type ? value : null

. No complicated rules to memorize. Use Type(value)

only when you want to convert a value of one type to a value of another type.

+3


source







All Articles