Bug 201695

Summary: Function declaration statements in false conditional contexts create working functions and assign them to identifiers in the parent context.
Product: WebKit Reporter: Dave Livesay <dlivesay>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: fpizlo, mark.lam, webkit-bug-importer, ysuzuki
Priority: P2 Keywords: InRadar
Version: Safari 13   
Hardware: Unspecified   
OS: Unspecified   

Description Dave Livesay 2019-09-11 13:33:59 PDT
The following statement
    if (false) {
        function myFunction(){
            return "I work!";
        }
    }
should declare a variable, "myFunction," in the parent namespace, if it doesn't already exist, but it should neither assign a value to the variable nor create a function. This behavior is confirmed, for example, in the Google Chrome console:
 > if(false){function myFunction(){return "I work!";}}
 < undefined
 > "myFunction" in window
 < true
 > typeof myFunction
 < "undefined"
And attempting to invoke myFunction throws a TypeError error.

In the Safari console, however, a working function is created and assigned to the variable:
 > if(false){function myFunction(){return "I work!";}}
 < undefined
 > "myFunction" in window
 < true
 > typeof myFunction
 < "function"
 > myFunction()
 < "I work!"

The behavior is the same if the variable already exists and has a value in the parent namespace:
 > myFunction = "just a string"
 < "just a string"
 > typeof myFunction
 < "string"
 > if(false){function myFunction(){return "I work!";}}
 < undefined
 > typeof myFunction
 < "function"
Thus, the existing value is overwritten with the function.
Comment 1 Radar WebKit Bug Importer 2019-09-13 20:23:06 PDT
<rdar://problem/55361190>