What is the difference between using class and function notation when creating a class?

Viewed 27

I'm having trouble finding information about the difference between these two ways of writing classes. Do they behave differently? why might you use one over the other ?

var Greeter = function(greeting) {
    this.greeting = greeting;
};

Greeter.prototype.greet = function() {
    console.log(this.greeting);
}

var hello = new Greeter("hello");
hello.greet();

versus:

class Greeter {
    greeting;

    constructor(greeting) {
        this.greeting = greeting;
    }
};

Greeter.prototype.greet = function() {
    console.log(this.greeting);
}

var hello = new Greeter("hello");
hello.greet();
0 Answers
Related