PDA

View Full Version : Phantom JavaScript warnings ...


brothercake
05-24-2005, 01:23 AM
Recently I ended up with some code that triggered an ambiguous warning in mozilla's JS console:

var a, b = true;

if(a = b) { alert('yes'); }

It looks wrong, but it's fine - it's a boolean test against b; the assignment to a just happens to be there.

So I got thinking - what else is there like that? Anyone got any more examples (in any language) of confusing syntax that looks like it's wrong, or throws an interpretor warning even though it's fine?

Sad I know ... but these things amuse me :)

enumerator
05-24-2005, 01:46 AM
if(+"1"+-1==false===true) alert("duh!") :confused:

Alex Vincent
05-24-2005, 05:07 PM
http://javascriptkit.com/javatutors/serror.shtml

liorean
05-25-2005, 03:15 AM
Type example for most C/C++ inspired languages:var
a=1,
b=1,
c=a+++b;
alert([a,b,c]);Is that postincrement a, add b; or is it add a to a preincremented b?

Or another version of same prob:var
a=1,
b,
c;
alert(++a+a++);
alert(a);Is that preincrement first a (a=2, return 2), add postincremented second a (a=3, return 2); or is it postincrement second a (a=2, return 1), add preincremented first a (a=3, return 3)?
In this case it'll make the same fallout, but what if we instead did alert((b=++a)+(c=a++));? What would b and c be?

Another issue with the same operators:var
a=1;
alert(a+++++a); throws an error, but alert(a++ + ++a); is just fine.


Note that in C, operand order of evalutation is NOT predefined, it's up to the compiler to chose which is best from optimisation purposes.

brothercake
05-25-2005, 02:34 PM
alert(a+++++a); throws an error, but alert(a++ + ++a); is just fine.

Lol, I like that one :D

Note that in C, operand order of evalutation is NOT predefined, it's up to the compiler to chose which is best from optimisation purposes.

Really? Doesn't that a get a bit, well, ambiguous?