gpt4 book ai didi

node.js - 从回调访问调用者参数

转载 作者:搜寻专家 更新时间:2023-11-01 00:18:53 26 4
gpt4 key购买 nike

我有这个:

var parseFile = function(err, data) {
// I need to access 'myFile'
};
fs.readFile(myFile, 'utf8', parseFile);

如何在别处声明的回调中访问 'myFile'

最佳答案

function parseFile(myFile) {
return function (err, data) {
// its a closure bound variable! yay.
}
}

fs.readFile(myFile, "utf8", parseFile(myFile))

关于node.js - 从回调访问调用者参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14790558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com