TypeError: Cannot read property 'name' of undefined


#1

Hi, I’m having trouble with the exercise 10 (The This Keyword I) and have no clue what is going on. The problem is with line 7 where I attempt to access the “name” property of “person” using “this.name”. I have included the code and the full error output so you can see. It seems it should just access the “name” property of “person” but I get “undefined” instead. Any ideas?

Here is all the code:

let person = {
  name: "Timothy",
  age: 28,
  weekendAlarm: 'No alarms needed',
  weekAlarm: 'Alarm set to 7AM',
  sayHello: () => {
    return `Hello, my name is ${this.name}`;
  },
  sayGoodbye() {
  	return 'Goodbye!';
	}
};

person.hobbies = ['model rocketry', 'programming'];
person.hobbies = ['model rocketry'];

let day = 'Monday';
let alarm;

if (day === 'Saturday' || day === 'Sunday') {
  alarm = 'weekendAlarm';
} else {
  alarm = 'weekAlarm';
}

console.log(person[alarm]);

console.log(person['name']);
console.log(person['age']);
console.log(person['hobbies']);
console.log(person.sayHello());

Here is the error output:

Oops! The test returned an error. Maybe you have a syntax error, or a typo. Hide error.
/home/ccuser/workspace/learn-javascript-objects-objects/main.js:9
    return 'Hello, my name is ' + undefined.name;
                                           ^

TypeError: Cannot read property 'name' of undefined
    at Object.sayHello (main.js:7:33)
    at main.js:31:20
    at Object.<anonymous> (main.js:31:1)
    at Module._compile (module.js:571:32)
    at loader (/home/ccuser/node_modules/babel-register/lib/node.js:158:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/ccuser/node_modules/babel-register/lib/node.js:168:7)
    at Module.load (module.js:488:32)
    at Object.load (/home/ccuser/node_modules/rewire/lib/moduleEnv.js:20:18)
    at internalRewire (/home/ccuser/node_modules/rewire/lib/rewire.js:57:15)
    at rewire (/home/ccuser/node_modules/rewire/lib/index.js:11:12)
    at Object.<anonymous> (test.js:3:15)
    at Module._compile (module.js:571:32)
    at loader (/home/ccuser/node_modules/babel-register/lib/node.js:158:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/ccuser/node_modules/babel-register/lib/node.js:168:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at /home/ccuser/node_modules/mocha/lib/mocha.js:220:27
    at Array.forEach (native)
    at Mocha.loadFiles (/home/ccuser/node_modules/mocha/lib/mocha.js:217:14)
    at Mocha.run (/home/ccuser/node_modules/mocha/lib/mocha.js:469:10)
    at Object.<anonymous> (/home/ccuser/node_modules/mocha/bin/_mocha:404:18)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)
    at bootstrap_node.js:542:3

#2

Arrow functions do not have a context object, so no this. Use a method similar to sayGoodbye().

ASIDE

ES6 and above have done away with the function keyword on methods (see, sayGoodbye()).

this.sayHello: function () {
    return 'Hello, my name is ' + this.name;
}

becomes,

sayHello () {
    return `Hello, my name is ${this.name}`;
}

#3

Thank you, that was the problem. doing away with the arrow operator fixed it.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.