小编Err*_*lva的帖子

NodeJS,Express,Prototyping中的变量范围问题

文件:MainApp.js

var reqHandler = reqire('HTTPRequestPostHandler')..
...
...
var httpRequestHandler = new reqHandler();


app.post('/', httpRequestHandler.handleRootPost);
Run Code Online (Sandbox Code Playgroud)

文件:HTTPRequestPostHandler.js

HTTPRequestPostHandler =function(){
   this.someVar = value;
}
HTTPRequestPostHandler.prototype.handleRootPost{
     console.log(this.someVar) //Error -> this.someVar is undefined.

}
Run Code Online (Sandbox Code Playgroud)

我有这两个文件.MainApp.js是express配置的地方,每个端点的各种处理程序,例如'/'.

但是当发出post请求并调用请求处理程序(HTTPRequestPostHandler.prototype.handleRootPost)时,访问变量this.someVar时出现未定义的错误.

为什么会这样呢?我在这做错了什么.

javascript prototype node.js express

0
推荐指数
1
解决办法
124
查看次数

标签 统计

express ×1

javascript ×1

node.js ×1

prototype ×1