I don't know why I get an error "this.getName is not a function" when I use arrow function for the object. When I use normal function, the output is good.
Object with normal function. All good.
var person = {
name: 'Juan',
lastname : 'Rodríguez',
getName: function() {
return this.name;
},
getLastname: function() {
return this.lastname;
},
fullname:function () {
return this.getName() + " " + this.getLastname()
}
}
console.log(person.fullname()); // good output Juan Rodríguez
However with arrow function () => , I get error as "this.getName is not a function"
var person = {
name: 'Juan',
lastname : 'Rodríguez',
getName: () => {
return this.name;
},
getLastname: () => {
return this.lastname;
},
fullname: () => {
return this.getName() + " " + this.getLastname()
}
}
console.log(person.fullname()); // bad output this.getName is not a function
Any solution so that I do not get the error? If I want to use arrow functions?