我这里有这个代码:
var myRequest = new httpReq.myRequest(buffer,socket);
if(!myRequest.hasSucceded()){ //return error
return;
}
arr=myRequest.getCookies();
....
我肯定在myRequest对象上有这个功能:
function myRequest(buffer, socket) {
...
this.cookies = new Array();
...
//returns the cookie array
function getCookies() {
return this.cookies;
}
...
}
exports.myRequest = myRequest;
我得到一个错误说:
TypeError: Object #<myRequest> has no method 'getCookies'
Y U NO GIVE COOKIES ???
请帮助...答案 0 :(得分:3)
您将getCookies声明为本地函数。
要在外部调用它,您需要在对象上创建属性getCookies,以便可以调用它。
试试这个:
function myRequest(buffer, socket) {
...
this.cookies = new Array();
...
//returns the cookie array
this.getCookies = function() {
return this.cookies;
}
...
}
您也可以myRequest.cookies
代替myRequest.getCookies()
答案 1 :(得分:2)
function myRequest(buffer, socket) {
this.cookies = [];
}
myRequest.prototype.getCookies = function () {
return this.cookies;
};
首先,在构造函数中声明getCookies直接导致此方法成为私有方法,该方法仅存在于构造函数中。
其次,通常认为在构造函数之外定义原型getCookies是一种好习惯。如果它在构造函数中定义(例如,this.getCookies = function(){...}),则每次实例化此原型时都需要初始化此方法。