Javascript Objects 10/15 - the "this" Keyword I


#1

I am not getting it … it is showing error!! why??

image

please help!!

thank you!


#2

can you copy paste your code to the forum? The error might be caused by a mistake in the line above, so without the full code its impossible to day


#3

/home/ccuser/workspace/learn-javascript-objects-objects/main.js:8
return Hello, my name is ${tyron};
^

ReferenceError: tyron is not defined
at Object.sayHello (/home/ccuser/workspace/learn-javascript-objects-objects/main.js:8:33)
at Object. (/home/ccuser/workspace/learn-javascript-objects-objects/main.js:23:20)
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)


#4

this was the error i was getting


#5

this part of the error message:

ReferenceError: tyron is not defined

says you have an undefined variable (tyron)

If you want more help, post your full code (like i asked before)


#6

how to post it?? …


#7

you copy paste the code you have to the forum, very similair to what you just did with the error message


#8
let person = {
  name: 'Tyron',
  age: 40,
  weekendAlarm: 'No alarms needed',
  weekAlarm: 'Alarm set to 7AM',
  
  sayHello: function() {
    return `Hello, my name is ${tyron}`;
  },
  
  sayGoodbye() {
    return 'Goodbye!';
  }
};


let friend = {
  name: 'Lebron'
};

friend.sayHello = person.sayHello;

console.log(friend.sayHello());
console.log(person.sayHello());

person.hobbies = ['Basketball', 'Coaching'];
person.hobbies = ['Basketball'];
console.log(person.hobbies);


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

let day = 'Tuesday';
let alarm;

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

console.log(person[alarm]);

#9

in our say hello method:

  sayHello: function() {
    return `Hello, my name is ${tyron}`;
  },

we want to reference the name property we defined here:

name: 'Tyron',

in the information section of the issue is addressed how we access properties:

To address this scope issue, we can use the this keyword to access properties inside of the same object.

so if we want to access the name property, we need …?


#10

soo…what should i do??


#11

name is the property we want to access in the sayHello method, to access a properties in a method we need to use this.propertyName, so what should you do?


#12

soo …i have to write this.name
right??


#13

yes, that is what you have to write within the ${} to print the name


#14

yess…now i get it…
thanks bro::smiley::smiley::smiley:


#15