Usually when we use this, it refers to the class.
But in this case, this is dataChannel, how can I let this refers to VideoService again? Thanks
export class VideoService {
dataChannel:any;
setupPeerConnection() {
this.dataChannel.onopen = this.dataChannelStateChanged;
}
dataChannelStateChanged() {
// here this = dataChannel, how can I let this = VideoService
console.log(this);
}
}
You could use bind.
setupPeerConnection() {
this.dataChannel.onopen = this.dataChannelStateChanged.bind(this);
}
bind creates a copy of a function with the specified object set as this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With