Bug 33763 - Defining functions with "if(true) else" clause, results in the last function to be defined.
Summary: Defining functions with "if(true) else" clause, results in the last function ...
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: PC Windows Vista
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-16 11:12 PST by Ben Gerrissen
Modified: 2010-01-16 15:06 PST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ben Gerrissen 2010-01-16 11:12:50 PST
Try:

if(true) {
   function test() {
      alert("SUCCESS!");
   }
} else {
   function test() {
      alert("FAIL!");
   }
}

test(); // alerts "FAIL!"

Not sure if I listed the correct webkit version, this bug happens for me in Windows Safari 4.0.4 (531.21.10)
I suspect all function declarations are evaluated ignoring the if/else clause.

Bug mitigation, declare functions as variables...

if(true) {
   var test = function() {
      alert("SUCCESS!");
   }
} else {
   var test = function() {
      alert("FAIL!");
   }
}

test(); // alerts "SUCCESS!"
Comment 1 Ben Gerrissen 2010-01-16 15:06:25 PST
Appologies, learned the difference between declarations and expressions.
This is exactly according to ECMA 262 spec and is a developer error.