1
이벤트 수신시 프로토 타입 함수를 트리거하는 간단한 클래스를 작성하려고하지만 범위에 문제가 있습니다. 어떤 이유로 인해 이벤트 리스너를 바인딩 한 경우에도 Stream
컨텍스트에 액세스 할 수 없습니다. onData
내EventEmitter에 의해 프로토 타입에서 프로퍼티에 액세스하기
function Stream(report) {
this.report = report;
this.stream = new api.getTagStream(report.tag);
this.stream.on('error', this.onError.bind(this));
this.stream.on('data', this.onData.bind(this));
return this;
}
Stream.prototype.onError = err => {
// Had an error
}
Stream.prototype.onData = data => {
console.log(this.report); // undefined
}
new Stream({ tag: 'sometag' });
로그인 그러나 정의되지 않은 반환, 보고서 개체를 표시해야합니다.