so I'm a little confused when it comes to the super() function within a class.
It seems like when I want to return this.name when my event is emitted, it returns undefined.
enter class Girl extends EventEmitter{
constructor(name, eyeColor, hairColor){
super()
this.name = name;
this.eyeColor = eyeColor;
this.hairColor = hairColor
}
}
var hailey = new Girl("Hailey", "Green", "Black")
hailey.on("messageLogged", (callback)=>{
callback(this.name)
});
hailey.emit("messageLogged", (msg)=>{
console.log(msg)
})
//returns undefined in console
But when I return the created instance of the class by property name it works.
class Girl extends EventEmitter{
constructor(name, eyeColor, hairColor){
super()
this.name = name;
this.eyeColor = eyeColor;
this.hairColor = hairColor
}
}
var hailey = new Girl("Hailey", "Green", "Black")
hailey.on("messageLogged", (callback)=>{
callback(hailey.eyeColor)
});
hailey.emit("messageLogged", (msg)=>{
console.log(msg)
}) //returns Green
Not sure if this is working as intended or I am missing something as this is my first attempt at using the extends and super keywords.
When I look at examples I have done with classes in the past I reference it by the newly created instance, but I just wanted to double check with StackOverflow and developers who are more confident than myself.