1
Node.js를 사용하여 웹 응용 프로그램의 서버 측을 작성하려고합니다. 다음 코드는 상황을 시뮬레이트하기 위해 추출됩니다. 문제는 actionExecuted "메서드"에서 this.actions.length에 액세스하려고 할 때 응용 프로그램이 충돌한다는 것입니다. this.actions 속성은 "생성자"(요청 함수 자체)에 정의 된 경우에도 정의되어 있지 않습니다 (범위 내에서 == {}). 액션 속성을 다른 "메소드"에서도 액세스 가능하게 만드는 방법은 무엇입니까?Javascript - "this"가 비어 있습니다.
var occ = {
exampleAction: function(args, cl, cb)
{
// ...
cb('exampleAction', ['some', 'results']);
},
respond: function()
{
console.log('Successfully handled actions.');
}
};
Request = function(cl, acts)
{
this.client = cl;
this.actions = [];
this.responses = [];
// distribute actions
for (var i in acts)
{
if (acts[i][1].error == undefined)
{
this.actions.push(acts[i]);
occ[acts[i][0]](acts[i][1], this.client, this.actionExecuted);
}
else
// such an action already containing error is already handled,
// so let's pass it directly to the responses
this.responses.push(acts[i]);
}
}
Request.prototype.checkExecutionStatus = function()
{
// if all actions are handled, send data to the client
if (this.actions == [])
occ.respond(client, data, stat, this);
};
Request.prototype.actionExecuted = function(action, results)
{
// remove action from this.actions
for (var i = 0; i < this.actions.length; ++i)
if (this.actions[i][0] == action)
this.actions.splice(i, 1);
// and move it to responses
this.responses.push([action, results]);
this.checkExecutionStatus();
};
occ.Request = Request;
new occ.Request({}, [['exampleAction', []]]);
결국 나는 그것을 다른 방법으로 해결했지만 문제를 지적했습니다. 감사. – Kaspi