作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 Koa 中,我可以通过 this
访问第一个生成器函数中的 Koa Context:
app.use(function *(){
this; // is the Context
}
但是如果我让步给另一个生成器函数,我就不能再通过 this
访问上下文了。
app.use(function *(){
yield myGenerator();
}
function* myGenerator() {
this.request; // is undefined
}
我已经能够简单地将上下文传递给第二个生成器函数,但想知道是否有更简洁的方法来访问上下文。
有什么想法吗?
最佳答案
要么像你说的那样将 this
作为参数传递:
app.use(function *(){
yield myGenerator(this);
});
function *myGenerator(context) {
context.request;
}
或使用 apply()
:
app.use(function *(){
yield myGenerator.apply(this);
});
function *myGenerator() {
this.request;
}
关于node.js - 如何在另一个生成器函数中访问 koa 上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26643630/
我是一名优秀的程序员,十分优秀!