What is the difference between `function () {}` and `() => {}` in JavaScript?

20 Views Asked by At

I am trying to add a method to the Person constructor function but keep getting an output of "Hello undefined"


    function Person(name, age) {
        this.name = name;
        this.age = age;
    }

    Person.prototype.sayHi = () => {
        return (`Hello ${this.name}`);
    }

    let person1 = new Person("Jean", 30);

    console.log(person1.sayHi());

    //Output when I use `function () {}` is "Hello Jean.
    //Output when I use `() => {}` is "Hello undefined".
0

There are 0 best solutions below