Access overridden global variable inside a function

前端 未结 2 947
心在旅途
心在旅途 2020-12-11 18:11

I want to access global variable \'x\' when it is over-ridden by same named variable inside a function.

function outer() {
   var x = 10;
   function overRid         


        
2条回答
  •  余生分开走
    2020-12-11 18:59

    The global scope of your web page is window. Every variable defined in the global scope can thus be accessed through the window object.

    var x = 10;
    function overRideX() {
        var x = "Updated";
        console.log(x + ' ' + window.x);
    }();
    

提交回复
热议问题