Why does IE nuke window.ABC variables?

前端 未结 3 1675
心在旅途
心在旅途 2020-12-01 08:03

When running the following block of code, FF and Chrome output typeof(hiya) = string while IE7/8 output typeof(hiya) = undefined.

&         


        
3条回答
  •  醉梦人生
    2020-12-01 08:29

    What you encountered is due to:

    1. var being a statement
    2. There's no block scope in JS
    3. Statements get executed before the code runs

    So what happens is that JavaScript will execute the var statement before before anything else, but it will not evaluate the assignment expression, therefor hiya will default to the value of undefined.

    As Raynos already stated IE will execute each scripts on its own, therefore the behavior described above, will results in hiya being undefined.

提交回复
热议问题