window.variableName

前端 未结 6 1816
梦如初夏
梦如初夏 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 06:56

    It is used to define global variable in JavaScript.

    globalVar = "Hello World";
    function function1(){
        alert(window.globalVar);
    } 
    function1();
    

    This will print "Hello World" in the popup.

        function function1(){ 
            globalVar = "Hello World";
            alert(window.globalVar);
        }function function2(){
            alert(window.globalVar);
        } 
        function1(); 
        function2();
    

    This will create two popups with value "Hello World", one from function1() and another from function2().

    So, by using window we can call a variable from any scope in javascript.

提交回复
热议问题