简体   繁体   中英

Where does this 'file' argument come from?

var filterFn = require('./solution_filter.js')
var dir = process.argv[2]
var filterStr = process.argv[3]

filterFn(dir, filterStr, function (err, list) {
  if (err)
    return console.error('There was an error:', err)

  list.forEach(function (file) {
    console.log(file)
  })
})

I understand the code up to the point where the forEach function is called and the file argument is passed in. The file argument isnt defined anywhere and I just dont understand where it comes from or what role it plays in this code. I would much appreciate it if someone could clarify this for me. Thank in advance. PS: This is from a series of workshops on node.js called leanryounode.

The forEach function will pass in the currentValue, index, and array.

Since the defined function only names one variable (file), it will basically serve as an alias for currentValue, which will then be accessible in the function body.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

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