On 14/03/11 9:36 PM, Bruno Jouhier wrote:
I found difference between JS engines in the way they handle code like the following:(function() { foo("m1a"); function foo(m) { alert("f1: " + m); } foo("m1b"); if (true) { foo("m2a"); function foo(m) { alert("f2: " + m); } foo("m2b"); } foo("m3a"); function foo(m) { alert("f3: " + m); } foo("m3b"); })(); In Chrome, I get f3: m1a, f3: m1b, f3: m2a, f3: m2b, f3: m3a, f3: m3b In FF 4, I get f3: m1a, f3: m1b, f3: m2a, f2: m2b, f2: m3a, f2: m3b If I simplify this to: (function() { foo("m1"); if (true) { foo("m2a"); function foo(m) { alert("f2: " + m); } foo("m2b"); } foo("m3"); })(); I get the following result: Chrome: f2: m1, f2: m2a, f2: m2b, f2: m3 FF4: foo is not defined It looks like Chrome applies a very simple rule, which is to take the last definition encountered in the scope (ignoring all {} which don't really create a scope) while Firefox applies a more sophisticated rule. Is this behavior precisely defined by the ECMAScript standard, or is it left open to interpretation? Bruno
I think i read in the Google Javascript style guide that for situations like the one above, functions should be declared like:
var f = function... I guess this is so var hoisting does the "right thing" for you? Regards, -- Shaun -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
