Classes in JS? What am I doing?

What am I doing wrong?

What they ask:

Make this code work
Write a class named Person that would make this code work:

NOTE: you may use ES5 constructor functions or ES6 class syntax

const hercules = new Person(‘Hercules’, ‘Mulligan’)
hercules.fullName() // => return “Hercules Mulligan”

const john = new Person(‘John’, ‘Adams’)
john.fullName() // => return “John Adams”

My code:

class Person {
    constructor(fullName){
        this.fullName = fullName
    }
    this.fullName()
}

const hercules = new Person('Hercules', 'Mulligan')
hercules.fullName() // => return "Hercules Mulligan"

const john = new Person('John', 'Adams')
john.fullName() // => return "John Adams"

The constructor describes the properties that are going to be on the object you create when you call new MyClass. It’s just a function that returns an object.

This:

function Person(fullName) {
  this.fullName = fullName;
}

Is the same as

class Person {
  constructor(fullName) {
    this.fullName = fullName;
  }
}

Both of them, when you run new Person('Harold Human'), will return an object that looks like:

{ fullName: 'Harold Human' }

You have a function that takes some arguments, and anything you assign to this.someProperty will appear under the key someProperty in the object you can create with the function.

You have a rogue extra line of code in your class that is a syntax error and will cause it to blow up. And the function takes two arguments, the property fullName is going to need to join those two things together.

EDIT: I just read this back, and I mistyped at the end there, and missed out something, so for future reference for anyone looking at this thread

the property fullName is going to need to join those two things together.

should have been

the method fullName is going to need to join those two things together.

And I should have added what a method looks like:

The properties you add to the object can be functions, and in this case it is asking you to add a function that joins the first and last names together. The way you define them is like this:

class Person {
  constructor(fullName) {
    this.fullName = fullName;
  }

  // method
  greet() {
    return `Hello ${this.fulllName}`;
  }
}

They are added in addition to the constructor function, and allow you to access/update properties defined on the object (like this.fullName).

Do you just mean first and last name? How do I join them?

Not sure which challenge are you taking on, but it seems it’s asking you to create a new method in that class that will return firstName + lastName. Remember to use the “this” keyword, like this.firstName this.lastName.

You also need to modify your class’s constructor parameters to accept two parameters. Right now you’re only taking one argument(fullName) but passing in two when you create a new class instance (‘Hercules’, ‘Mulligan’)

Here’s a simple example that includes declaring a method inside a class:

class Dog {
    constructor(name, legs) {
        this.name = name
        this.legs = legs
    }

    getSummary() {
      console.log(`Greetings, human! My name is ${this.name} and I have ${this.legs} legs`)
    }
}

const myDog = new Dog(`Awesome Dog`, 9001)
myDog.getSummary() // Greetings, human! My name is Awesome Dog and I have 9001 legs

I got it, thanks anyway guys!

class Person {
    constructor(firstName, lastName){
        this.firstName = firstName
        this.lastName = lastName
    }
    fullName(){
        let fullName = `${this.firstName} ${this.lastName}`
        return fullName
    }
}
1 Like

Sorry, I just read my reply back and the last bit was probably slightly confusing as I wrote property instead of method & didn’t provide an example (edited for future reference of anyone reading the thread anyway) :confused: