Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过字符串检查函数闭包中是否存在函数_Javascript_Node.js_Closures - Fatal编程技术网

Javascript 通过字符串检查函数闭包中是否存在函数

Javascript 通过字符串检查函数闭包中是否存在函数,javascript,node.js,closures,Javascript,Node.js,Closures,在JavaScript异步函数中,我想检查某个函数在其闭包环境中是否可用 该功能可用。我可以显式执行它,或者执行console.logmyClosureScopedFunc 但是,当函数名位于字符串变量中时,如何查看闭包中是否存在函数名 在异步函数中,这是未定义的,否则我可以这样做,如果这个['myClosureScopedFunc']//gotcha 出于某些可能很明显的原因,我不能在与myClosureScopedFunc相同的范围内执行self=this,因为self也将在异步函数中未定义

在JavaScript异步函数中,我想检查某个函数在其闭包环境中是否可用

该功能可用。我可以显式执行它,或者执行console.logmyClosureScopedFunc

但是,当函数名位于字符串变量中时,如何查看闭包中是否存在函数名

在异步函数中,这是未定义的,否则我可以这样做,如果这个['myClosureScopedFunc']//gotcha 出于某些可能很明显的原因,我不能在与myClosureScopedFunc相同的范围内执行self=this,因为self也将在异步函数中未定义。 eval'myClosureScopedFunc'可以工作,但我不想使用eval。 简约表达代码示例

要查找的路由函数在req.params.route中定义

这是答案 编辑到问题中,因为它已作为半相关案例的副本关闭。如果有人通过google来到这里,下面是您在Node.js模块上下文中具体的操作方法

我最初的第二个想法是正确的,但正如commenter@Bergi所指出的,this关键字的作用域是方法

因此,每个函数都需要添加到导出对象中。然后我们可以按预期使用它:

'use strict'

const self = this // scope the module
exports.myClosureScopedFunc = myClosureScopedFunc // Add to scope

module.exports = async function(req, res, next) {
    try {
        if (self[req.params.route].length === 3)
            return await self[req.params.route](req, res, next)
    }
    catch(err) {
        console.log(err.stack)
        return res.status(404).end(err.message)
    }
}

async function myClosureScopedFunc(req, res, next) {
    return await "some async data"
}

模块上定义的函数不向全局对象公开,因此要按名称访问这些函数,需要将它们存储在对象中

在下面的代码段中,定义了一个funcs对象,该对象包含私有函数,无法从此模块外部访问,因为funcs从未导出

功能要求{ 常量funcs={ 异步myClosureScopedFuncreq,res,next{ 返回等待一些异步数据 } }; //或 //funcs.myClosureScopedFunc=异步函数…{} 返回异步函数exportedFuncname{ iffuncs[名称] console.logawait funcs[名称]; 其他的 console.log'nop'; } } 常数x=要求; x‘imnota函数’;
x'myClosureScopedFunc';我不明白这与async/await有什么关系。在普通函数中也是如此。JS使用词法范围。您不必检查它在运行时是否可用。在编译时和实现过程中,您知道它是否在范围内!你的目标是什么?听起来你真的在寻找eval,所以请解释一下你想要的东西与eval有什么不同。@Bergi你是对的。它在非异步模式下也不可用。我错了。为什么这个函数中没有这个?异步函数和普通函数中也没有什么特别的地方。如果这不是您期望的值,那么您就不能将函数作为方法调用。谢谢@Bergi,您帮助我找到了答案。我在上面加了它;由于问题已锁定,我无法将其作为答案发布。
'use strict'

const self = this // scope the module
exports.myClosureScopedFunc = myClosureScopedFunc // Add to scope

module.exports = async function(req, res, next) {
    try {
        if (self[req.params.route].length === 3)
            return await self[req.params.route](req, res, next)
    }
    catch(err) {
        console.log(err.stack)
        return res.status(404).end(err.message)
    }
}

async function myClosureScopedFunc(req, res, next) {
    return await "some async data"
}