Actionscript 2 Mystery of the day
I was just wondering if anyone could give me a good reason why
trace( (0 || 23) );
would trace '23' instead of 'true' in AS2.
Bonus points for answers that contain mythical code-gnomes.
I was just wondering if anyone could give me a good reason why
trace( (0 || 23) );
would trace '23' instead of 'true' in AS2.
Bonus points for answers that contain mythical code-gnomes.
August 22nd, 2006 at 4:28 pm
the way flash handles boolean ORs is it evaluates the first half. If it evals to true, it returns the first half. If it evals to false, it returns the second half. It doesn't convert what it returns, just returns the raw value. If you actually had boolean values in there, trace(false || true) it would return the second value, true. But since 0 evaluates to false, it just returns 23.
August 22nd, 2006 at 4:38 pm
Weird,
anyway, it works this way:
var result:Boolean = Boolean(0 || 1);
trace(result);
August 22nd, 2006 at 5:04 pm
That's a good try Keith, but you forgot the part about the code-gnomes toiling for hours in the caves of Ecma to bring us these return values. Reportedly, many gnomes perish each day just to maintain the supply of Boolean return values we developers consume in just one debug session.
August 23rd, 2006 at 1:09 pm
You have to enter 4 8 15 16 first, THEN 23, followed by 42!
Confused?! Watch the orientation film produced by Hanso Corporation.
August 24th, 2006 at 10:56 pm
Weird,
trace((Number(0) || Number(23))); // returns 23
trace((Number(0) == false ? 0 : 23)); // returns 0
Should Flash fundamentally be doing the same evaluation on both traces?
The first trace says, if the first part is true, return it, if not, return the second half. The second trace is doing the same thing, except Im spelling it out for flash. Because, Flash evaluates Number(0) as false.
Oh yea, no code-gnomes were harmed in the execution of this experiment.
August 30th, 2006 at 5:38 pm
trace( (0 || 23) ); //return 23
trace( (1 || 23) ); //return 1
trace((Number(1) == true ? 0 : 23)); // returns 0
mistery solved, but
a question ... what are code-gnomes ?
Are like ants, or the small mouse that make cofe in the automated machine
here in italy? Are code-gnomes that bring me the result of a formula ?
(somtime they are drunken ! )
April 3rd, 2007 at 6:50 pm
Actually, there is a "bug?" in Actionscript implementation.. The following code doesn't work:
function a(){return "a"};
function b(){return "b"};
trace ( (a||b) () ); // this should trace "a" but traces "undefined";
trace ( Function(a||b)()) ; //this DO trace "a";
December 5th, 2007 at 2:36 pm
October 28th, 2009 at 10:49 am
the OR checks to see if the first item is TRUE if it is, returns THAT element...
is like...
function OR(elementA:*, elementB:*):*
{
if(Boolean(elementA))
return elementA
else
return elementB
}