如何在 writable.write 函数中使用 "this"
How to use "this" inside within writable.write function
我需要在 writable.write 函数中访问一些 class 实例数据。这是一个简短的打字稿代码片段,说明了我正在尝试做的事情:
import * as socketio from 'socket.io'
import { Writable } from 'stream'
export class DataClient {
public socket: socketio.Socket
private writable: Writable
constructor(socket: socketio.Socket) {
this.socket = socket
this.writable = new Writable({
write: function (chunk, encoding, next) {
this.socket.emit('data', chunk)
next()
}.bind(this),
})
}
}
我从 ESLint 得到以下错误:
any
'this' implicitly has type 'any' because it does not have a type annotation.ts(2683)
dataClient.ts(12, 14): An outer value of 'this' is shadowed by this container.
我试过使用 <> 和 as 进行转换,但这没有任何区别。实际代码更复杂,但这显示了最简单情况下的问题。此外,虽然我可能只能引用套接字(参数),但我还需要访问其他实例数据项,它们不是构造函数的参数。
有没有办法让 TS 知道“this”指的是 DataClient 实例?
您应该使用箭头函数来表示 write 方法,然后这将引用 DataClient 实例:
import * as socketio from "socket.io";
import { Writable } from "stream";
export class DataClient {
public socket: socketio.Socket;
private writable: Writable;
constructor(socket: socketio.Socket) {
this.socket = socket;
this.writable = new Writable({
write: (chunk, encoding, next) => {
this.socket.emit("data", chunk);
next();
},
});
}
}
另一种解决方案是将函数定义为 class 的方法:
import * as socketio from "socket.io";
import { Writable } from "stream";
export class DataClient {
public socket: socketio.Socket;
private writable: Writable;
constructor(socket: socketio.Socket) {
this.socket = socket;
this.writable = new Writable({
write: this.writeFunc,
});
}
writeFunc(chunk: any, encoding: BufferEncoding, next: any): void {
this.socket.emit("data", chunk);
next();
}
}
我需要在 writable.write 函数中访问一些 class 实例数据。这是一个简短的打字稿代码片段,说明了我正在尝试做的事情:
import * as socketio from 'socket.io'
import { Writable } from 'stream'
export class DataClient {
public socket: socketio.Socket
private writable: Writable
constructor(socket: socketio.Socket) {
this.socket = socket
this.writable = new Writable({
write: function (chunk, encoding, next) {
this.socket.emit('data', chunk)
next()
}.bind(this),
})
}
}
我从 ESLint 得到以下错误:
any
'this' implicitly has type 'any' because it does not have a type annotation.ts(2683)
dataClient.ts(12, 14): An outer value of 'this' is shadowed by this container.
我试过使用 <> 和 as 进行转换,但这没有任何区别。实际代码更复杂,但这显示了最简单情况下的问题。此外,虽然我可能只能引用套接字(参数),但我还需要访问其他实例数据项,它们不是构造函数的参数。
有没有办法让 TS 知道“this”指的是 DataClient 实例?
您应该使用箭头函数来表示 write 方法,然后这将引用 DataClient 实例:
import * as socketio from "socket.io";
import { Writable } from "stream";
export class DataClient {
public socket: socketio.Socket;
private writable: Writable;
constructor(socket: socketio.Socket) {
this.socket = socket;
this.writable = new Writable({
write: (chunk, encoding, next) => {
this.socket.emit("data", chunk);
next();
},
});
}
}
另一种解决方案是将函数定义为 class 的方法:
import * as socketio from "socket.io";
import { Writable } from "stream";
export class DataClient {
public socket: socketio.Socket;
private writable: Writable;
constructor(socket: socketio.Socket) {
this.socket = socket;
this.writable = new Writable({
write: this.writeFunc,
});
}
writeFunc(chunk: any, encoding: BufferEncoding, next: any): void {
this.socket.emit("data", chunk);
next();
}
}