What is the advantage of an untyped variable over an object? What is the difference between zero and undefined?

According to this: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9f.html Quote:

An untyped variable does not match a variable of type Object. The main difference is that untyped variables can contain the special value undefined, while a variable of type Object cannot hold this value.

However, when I test it as:

var objTest:Object = 123; var untypedTest:* = 123; objTest = undefined; untypedTest = undefined; //This is understandable but why was the assignment even allowed? trace(objTest); // prints null trace(untypedTest); // prints undefined objTest=null; untypedTest = null; //This is also understandable ... both can store null trace(objTest); // prints null trace(untypedTest); // prints null //If they are null whey are they being equal to undefined? if(objTest==undefined) trace("obj is undefined"); if(untypedTest==undefined) trace("untyped is undefined"); //Because null is same as undefined! if(null==undefined) trace("null is same as undefined?"); 

Two questions:

  • Why is undefined assignment allowed for obj? (not a big problem as it still prints as null)
  • If we compare null with undefined, the result is true (even if null is stored in the object). What is the point of making the difference between zero and undefined if they are equal?
+7
source share
2 answers
  • Flash has type conversion for converting some types.

Some examples of this:

 var i:int = NaN; trace (i); // 0 

Or:

 var b:Boolean = null; trace(b); // false 

Therefore, when you assign undefined to Object , the Flash instance converts it to null in the same way.

  • Your comparison applies type conversion to incompatible types before evaluating Boolean .

You can use strict comparison with false :

 if(null === undefined) trace("Never traced: null is not the same as undefined!"); 
+10
source

Some values ​​are converted unchanged for comparison or purpose.

One such conversion is converting undefined to null when upgrading to Object . Therefore, null == undefined , because in fact Object(null) == Object(undefined) executed and this is null == null .

However, if you perform a strict comparison, they are not converted and therefore not equal, i.e. null === undefined will return false.

0
source

All Articles