0

I have video stream code with blobs and MediaRecorder. And I'm trying to rewrite my old code on es6.

If I call the method without brackets then this.property does not work (returns undefined). If I call the method with brackets, "e" stops working.

class testClass {
  constructor() {
    this.blobdata = [];
  }

  startRecording() {
    this.mediaRecorder.ondataavailable = this.handleDataAvailable;
    // this.handleDataAvailable if try like that - 'this.blobdata' returns undefined in handleDataAvailable(e)
   // this.handleDataAvailable() if try like that - e.data return undefined...
  }

  handleDataAvailable(e) {
     // there is error (this.blobdata returns undefined) 
     if (e.data && e.data.size > 0) this.blobdata .push(e.data)
  }

}

let sc1 = new testClass();
let sc2 = new testClass();
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • 2
    `this.handleDataAvailable.bind(this)`? – jonrsharpe Apr 16 '20 at 13:08
  • That helped, now works fine. – jonrsharpe -- can u explain why? –  Apr 16 '20 at 13:10
  • At `this.mediaRecorder.ondataavailable = this.handleDataAvailable;` the context of the `handleDataAvailable` function is changed from the `testClass` to the `this.mediaRecorder`. So the `this.blobdata` becomes `undefined`. https://www.learn-js.org/en/Function_Context – subhasis Apr 16 '20 at 13:15
  • Thank u, now i understand –  Apr 16 '20 at 13:16

0 Answers0