Bug 201695 - Function declaration statements in false conditional contexts create working functions and assign them to identifiers in the parent context.
Summary: Function declaration statements in false conditional contexts create working ...
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: Safari 13
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2019-09-11 13:33 PDT by Dave Livesay
Modified: 2019-10-29 13:11 PDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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>