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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Javascript (howler js) How to pass child property to parent? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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']
});

Answer

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');
    }
});
We are here to answer your question about Javascript (howler js) How to pass child property to parent? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji