简体   繁体   中英

Access loop variable from within function

Not the greatest title i know, alright, consider:

for (var i = 0; i < map.length; ++i) {
place_ore(mountain_ore,mountain_allowed_ores) 
}

And inside place_ore() , I am trying to access map[i] ;, however when I try to do this, it gives me an undefined error. I think it has something to do with scope, but I can't quite work it out myself, any ideas?

Thanks.

You need to pass it in:

for (var i = 0; i < map.length; ++i) {
    place_ore(mountain_ore,mountain_allowed_ores, map[i]) 
}

And of course, modify your function signature:

function place_ore(mountain_ore,mountain_allowed_ores, mapTile) {
   //..place some ore in mapTile instead of map[i]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM