A problem with let block-level scope, which is inconsistent between safari and Chrome

if you have a question, add the code first:

if(1===1) {
    let nameSet = new Set();

    if (1 === 2) {
        // do nothing
    }
    else {

        function getArrayFromOl() {
            console.log("nameSet:", nameSet)
        }

        getArrayFromOl()
    }
}

this code reported an error in safari 11.0.3:

ReferenceError: Can"t find variable: nameSet

but the relevant variables can be accessed in Chrome.

I think we should also be able to access relevant variables.

is there a god who can explain why the error was reported under safari?


        if (1 === 1) {
            let nameSet = new Set();
            if (1 === 2) {
                // do nothing
            } else {
                console.log(nameSet)
                getArrayFromOl(nameSet)
                
                function getArrayFromOl(nameSet) {
                    console.log(nameSet)
                }
            }
        }

ps: function declares not to write in conditional statements, take it out

add: the
problem is mainly caused by the inconsistency between chrome and safari in the declaration of the function in the conditional statement. You can verify

with the following code
        test1() 
        if (true) {
            if (false) {

            } else {
                function test1() {
                    console.log('test') // chromesafari`test`
                }
            }
        }
The reason for the problem of the subject is that in safari , the getArrayFromOl function is equivalent to being promoted to the outermost if of , while nameSet is a let declaration, so the scope is in the first if of , so getArrayFromOl cannot get the corresponding value

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b32e7c-3421a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b32e7c-3421a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?