That is really weird, huh?
Well, for strings, I think it's often better to use .length for Boolean
tests, anyway:
function test(s:String) {
if (s.length) {
// Catches any nonempty string.
} else {
// Catches null, undefined, and empty strings.
}
}
Ok, this is why people have such a hard time with strict typing. First
of all, you should not be treating false, 0, null, and undefined as
interchangable, because they are not. This is the most basic of best
practices: say what you mean, and be explicit. Using an evaluation that is
not boolean as a boolean evaluation is just lazy code. Period. And later,
when you can't figure out where the bug came from, you'll spend hours
hunting down where a condition fell through the cracks. Oops, the new player
changed the implicit return value of the datatype you were using in your
condition, or changed the way different types are compared, and your whole
application broke as a result, when an extra 2 characters (>0) could've
avoided the issue.
Also, no one ever actually answered the OPs question. The difference
between null and undefined is that undefined means the variable was never
initialized or has been deleted. You should never *set* a variable to
undefined, you should always use null instead, otherwise you ruin the whole
distinction between an uninitialized variable and one that you have set to
null.
var a:Number;
trace(a); // undefined
a = 1; // set it
trace(a); // 1
a = null; // empty it
trace(a); // null
delete a; // uninitialize it
trace(a); // undefined
It is useful to have two different types of "no value assigned", because
you can tell the difference between a variable that is just currently
without a value, and one that has either never been assigned a value or has
been deleted. It is even more useful in complex datatypes.
var o:Object;
trace(o); // undefined
o = new Object(); // initialize
trace(o); // [Object object]
o = String("blah"); // assign a primitive as an object
trace(o); // blah
o = String(""); // set to empty string
trace(o); // (empty string, *not* undefined)
o = null; // empty it
trace(o); // null
delete o; // destroy it
trace(o); // undefined
So you can see, it can be useful to be able to make the distinction
between a variable that has been initialized and then emptied, and a
variable that has never been initialized. The most common place to do this
is in something like this:
class Example{
var o:Object;
function Example(){
// code
}
function SomeOtherFunction(){
// ensure the object has been initialized
// you may not want to reinitialize it if the value is null,
// since null may have some other meaning in this context.
if(o==undefined) o = new Object();
}
}
ryanm
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com