javascripthowler.js

Javascript (howler js) How to pass child property to parent?


Good day everyone, I'm fairly new to Javascript so please forgive me if this is a stupid question. I've been trying to google this, some say to use super() but I'm getting an error of undefined.

I'm using howlerJS. My goal is not to rewrite the same 'onend' listener/function whenever I instantiate a new Howl.

class ChildHowl extends Howl{
    ...how can i insert 'onend' prop here?...
}
var myHowl = new ChildHowl({
  src: ['audio.wav'],
  // how to move this onend to ChildHowl as a default
  onend: function() {
        console.log('audio ended');
        .... some stuff ....
      }
});

Thank you everyone in advance. You're understanding and consideration is highly appreciated.

Update: Fixed thank you Fabien Auréjac for the help!

class ChildHowl extends Howl{ 
  constructor(obj) { 
    let onend=function() { 
      console.log('audio ended'); 
    }; 
    super({ src:obj.src, onend:onend }); 
  } 
}

var myHowl = new ChildHowl({
  src: ['audio.wav']
});

Solution

  • Welcome, you can pass the argument via super(); in order to get the constructor of your parent class to receive it.

    class Howl {
        constructor(obj) {
            console.log(obj);
            this.src = obj.src;
            this.onend = obj.onend;
        }
    }
    class ChildHowl extends Howl{
        constructor(obj) {
            super(obj);
            this.onend();
        }
    }
    var myHowl = new ChildHowl({
        src: ['audio.wav'],
        onend: function() {
            console.log('audio ended');
        }
    });