Access overridden global variable inside a function

前端 未结 2 939
心在旅途
心在旅途 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 19:04

    You can use window.x to reference the globally scoped variable.

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

    This code logs "Updated" then 10.

提交回复
热议问题