Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.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 装饰师:“;这";在descriptor.value中访问时未定义_Javascript_Typescript_Decorator - Fatal编程技术网

Javascript 装饰师:“;这";在descriptor.value中访问时未定义

Javascript 装饰师:“;这";在descriptor.value中访问时未定义,javascript,typescript,decorator,Javascript,Typescript,Decorator,我正在试用decorators,我编写了一个decorator,它基本上返回一个新函数,该函数执行一些`console.log 这就是我的装饰师的样子: function test(target, name, descriptor) { const original = descriptor.value; console.log("bbau"); if (typeof original === 'function') { descriptor.value

我正在试用decorators,我编写了一个decorator,它基本上返回一个新函数,该函数执行一些`console.log

这就是我的装饰师的样子:

function test(target, name, descriptor) {
    const original = descriptor.value;
    console.log("bbau");
    if (typeof original === 'function') {
        descriptor.value = function (...args) {
            console.log(`Arguments: ${args}`);
            try {
                console.log("executing");
                const result = original.apply(this, args);
                console.log("done");
                console.log(`Result: ${result}`);
                return result;
            } catch (e) {
                console.log(`Error: ${e}`);
                throw e;
            }
        }
    }
    return descriptor;
}
这就是我使用它的方式:

class TestController extends BaseController<//..> {
    // ... 
    @test
    testIt(req: Request, res: Response) : Response {
       this.sendResponse();
    }

    sendResponse(options: ISendResponseOptions, res: Response) : Response {
       // return response
    }
}
类TestController扩展BaseController{
// ... 
@试验
测试(请求:请求,响应:响应){
这是sendResponse();
}
sendResponse(选项:ISendResponseOptions,res:Response):响应{
//返回响应
}
}
`` 但是,执行时会引发错误:
error:TypeError:无法读取未定义的
的属性“sendResponse”


有什么想法吗?谢谢

如果要从声明函数的上下文中捕获
(或者当
不重要时),通常应使用箭头函数。在这种情况下,您确实希望
this
成为调用函数的对象,因此您应该使用常规函数:

const test = (target, name, descriptor) => {
    const original = descriptor.value;
    if (typeof original === 'function') {
          descriptor.value = function (...args) {
            console.log(`Arguments: ${args}`);
            try {
                console.log("executing");
                const result = original.apply(this, args);
                console.log("done");
                console.log(`Result: ${result}`);
                return result;
            } catch (e) {
                console.log(`Error: ${e}`);
                throw e;
            }
        }
    }
    return descriptor;
}
你可以在实验室里测试一下

如果将此函数用作另一个函数的参数,则还应调用
bind
为该函数设置
this
(否则调用方将确定
this
的值):


使用正则(ES6之前)函数表达式语法,如@DavinTryon@DavinTryon刚刚更新了代码,不起作用如何调用
testIt
?@davintroon express:
router.route(“/”).post(testController.testIt)
@Sid我支持答案,这里不应该使用箭头函数。也许还有其他问题(如何调用函数)。我添加了一个游乐场链接,它演示了装饰师的工作原理。你对箭头函数的理解是绝对正确的,事实上,我正在尝试找出是否还有其他错误somewhere@Sid查看注释,添加了一个使用
绑定的解决方案(testController
您能花几分钟时间更新一下为什么它现在可以工作的问题吗?@Sid添加了一个简短的解释。它是否清楚地说明了为什么需要
bind
router.route("/").post(testController.testIt.bind(testController))