0

What is the difference between this:

class Animal {
    constructor(name) {
        this.name = name;
    }

    get name_of_animal() {
        return this.name;
    }
}

const dog = new Animal("Max");
console.log(dog.name_of_animal); // Max

And this:

class Animal {
    constructor(name) {
        this.name = name;
    }

    name_of_animal() {
        return this.name;
    }
}

const dog = new Animal("Max");
console.log(dog.name_of_animal()); // Max

I've also seen this:

class Animal {
    constructor(name) {
        this.name = name;
    }

    get name_of_animal() {
        return this.find_name_of_animal();
    }

    find_name_of_animal() {
        return this.name;
    }
}

const dog = new Animal("Max");
console.log(dog.name_of_animal); // Max

The last code looks useless, but what 'bout two above it?

Both of codes, first and second, returning the same value.

Thanks for reply.

John Doe
  • 1
  • 1

0 Answers0