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

后端 未结 4 640
灰色年华
灰色年华 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条回答
  •  -上瘾入骨i
    2020-12-17 16:08

    Unfortunately you can't. By naming the parameter in the nested function id, you've shadowed the parameter in the outer function. Javascript contains no facility for accessing the shadowed name. The only option is to choose a different name for one of the variables.

提交回复
热议问题