Having problems with the code (Javascript objects)


#1

I am having a problem with “The this keyword I - Objects” part of the course.

I am pretty sure that the code is correct but I am not sure.

In my code the line “return Hello, my name is ${this.name};” comes up in the console as “Hello, my name is undefined.” It also says that the this. is undefined.

I was wondering if I was doing it wrong or if this is a bug.


Javascript objects and methods with this. syntax
#2

Let’s pretend you are Bill Smith. You check into a hotel and become a guest. The staff will need to look up this guest on the computer to direct you to your room or retrieve messages from the hotel operator. Everyone in the chain of action knows that ‘this guest’ is Bill Smith.

We could give a more specific answer if the context of this question was better known. Please post a link to the exercise. Thanks.


#3

i have the code above it “name: ‘Alli’” in the object.
full code:

let day = 'Saturday';
let alarm;

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

person.hobbies = ['Dancing', 'Singing'];

if(day === 'Saturday' || day === 'Sunday'){
  alarm = 'weekendAlarm';
} else {
  alarm = 'weekAlarm';
}
  
person.hobbies = ['Dancing'];

console.log(person.sayHello());


console.log(person[alarm]);

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

#4

this used to be the method syntax:

var obj = {
  foo: function() {
    /* code */
  },
  bar: function() {
    /* code */
  }
};

then es6 was released, and methods could be shortened to:

var obj = {
  foo() {
    /* code */
  },
  bar() {
    /* code */
  }
};

using the arrow syntax for methods is not recommended, given this will keep referring to the window object, and not the current object, as such this.name is undefined. use the es6 syntax for methods


#5

Thanks for posting your code, though we still need the link…

Found what I think is the exercise,

Methods

Recall that this is an object, not a class so the property syntax still applies. Also note that your method is missing the arrow notation.

sayGoodbye: () => 'Goodbye!';

Notice that we write this on a single line with no block braces and no return keyword. The return is said to be implicit when the function is written on one line.

Also note that there is no this context since we are returning a string literal. Because it is a method of the object, we still need to call it in the object context.

console.log(person.sayGoodbye());    // Goodbye!

#6

So it’s me using the old method syntax? Ok, will fix. Thank you.


#7

Also I did put what the exercise is in the original post.

  • “The this keyword I” From Object in Javascript.

#8

you can use the es6 syntax for methods, that is fine:

var obj = {
  foo() {
    /* code */
  },
  bar() {
    /* code */
  }
};

but the arrow syntax shouldn’t be used for methods, for functions you can use the arrow key. For methods, its not recommend given this will not reflect the object


#9

And a single link would remove all confusion and rigor-marole trying to track it down. We do not have courses on speed dial. Please include a link when posting a lesson related question. Thanks.

As @stetim94 has stated, we can use ES6 class syntax on methods. My mistake.

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

The ES5 syntax would have looked like this…

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

As silly as it seems, the hybrid method from earlier is still valid, but I doubt of much use in light of the above unless you prefer the implicit return.

sayGoodbye: () => 'Goodbye!';

#10

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