window.variableName

前端 未结 6 1827
梦如初夏
梦如初夏 2020-11-28 06:45

I am going through some code and at the beginning of the script we have var emailID = email. Later on, the code refers to emailID by going window.emailID<

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 07:00

    window.myVar or window["myVar"] is an explicit way to refer to a global variable.

    A variable is a global variable if it's declared outside of a function (with or without "var"), or if it's declared inside a function without using "var", or if it's declared as window.myVar or window["myVar"].

    A variable is declared by either assigning a value to it, or by using the keyword var.

    One case where it's useful to refer to a global variable as window.myVar is if you're inside a function that has a local variable called myvar. In that case, myVar refers to the local variable, while window.myVar refers to the global variable.

提交回复
热议问题