JavaScript Access Local Variable with Same Name in Inner and Outer Scope

后端 未结 4 647
灰色年华
灰色年华 2020-12-17 15:53

Given the following JavaScript:

var someFunction = function(id) {
  //do some stuff
  var modifyId = function(id) {
     //do some stuff
     outer.id = id;          


        
4条回答
  •  臣服心动
    2020-12-17 16:29

    var someFunction = function(id) {
      //do some stuff
      var oid = id;
      var modifyId = function(id) {
         //do some stuff
         // you can access the outer id via the oid variable
      }
    }
    

    But, yes, you should just rename one of the formal parameters.

提交回复
热议问题