我有一个类及其定义的辅助类:
function ClassA(){
this.results_array = [];
this.counter = 0;
this.requestCB = function(err, response, body){
if(err){
console.log(err);
}
else{
this.counter++;
var helper = new ClassAHelper(body);
this.results_array.concat(helper.parse());
}
};
};
function ClassAHelper(body){
this._body = body;
this.result_partial_array = [];
this.parse = function(){
var temp = this.parseInfo();
this.result_partial_array.push(temp);
return this.result_partial_array;
};
this.parseInfo = function(){
var info;
//Get some info from this._body
return info
};
};
NodeJS 给我以下错误:
TypeError: Object #<Object> has no method 'parseInfo'
我不明白为什么我不能从 ClassAHelper 的解析方法内部调用 this.parseInfo() 。
如果有人能解释一个可能的解决方案。或者至少,问题是什么?我尝试重新排序函数声明以及其他一些想法,但无济于事。
附注我尝试简化 stackoverflow 的代码。幸运的是它仍然有意义:)
P.P.S 这是我的第一个 stackoverflow 问题。希望我做的一切都是正确的。 :)
最佳答案
这是一个有效的简化示例:
function A() {
this.x = function (){
return this.y();
};
this.y = function (){
return "Returned from y()";
};
}
var a = new A();
a.x();
注意 new
的使用并使用 a.x()
调用该方法。
如何在 ClassAHelper
中创建函数实例并调用 parse
?
是这样的吗:
var a = A();
a.x();
// Or
A.x()
关于node.js - NodeJs 给我一个对象 #<Object> 没有方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29549201/