Trouble with learning syntax for functions in objects


#1

I know this site is all about learning by doing yourself, but I have been making the same syntax problem over and over again.

I just really don’t know what to do.

Here is the problem.

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

Like I said, I know it’s messy but would like to know the correct answer so that I can learn to do it on my own. I’m trying to choose my battles.

I made a topic similar to this one but cant find the thread. Sorry in advance.

the link


#2

I’d like to reiterate that I’m trying to learn how to do it right from the beginning so that I don’t learn how to do it the wrong way.


#3

Ive solved it but I think it will stil be helpful to know what I did wrong.


#4
let person = {
  name: 'JP',
  weekendAlarm: 'No alarms needed',
  weekAlarm: 'Alarm set to 7AM',
  
  sayHello: function() {
    return 'Hello, there!';
  // missing } to close method
  sayHello: function() {
    return `Hello, my name is ${this.name}`;
  } // missing , to separate properties
  
  
  sayGoodbye() {
    return 'Goodbye!';
  }
  
};


i added two comments to the code. The first comment described how you forgot a } to close of your method. When you get an error, do not only look at the error message line, but also above

the second comment mentioned you forgot to add a , to separate the properties. You do this fine with name and so on, but methods are also properties, so you need , after the } which closes a method

the second comment obviously also applies to the first comment

I thought we talked about this:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions#Description

Its confusing that you use both es5 and es6 syntax for defining methods


#6

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