0

undefined is printed instead of "Tester"

var sayName = () => {
    console.log('My name is ' + this.name);
};

const person = {
    'name': 'Tester',
    'age': 34
};

sayName.call(person)

Expected to print "

My name is Tester

" but got

My name is undefined
  • 3
    Arrow functions don't have their own `this`. Change it to a regular function and it will work. – CherryDT Dec 25 '22 at 10:58
  • 1
    If you change it from a function expression to a function declaration it will work since arrow functions don't maintain their own `this` context. – Andy Dec 25 '22 at 11:00

0 Answers0