1

consider the following snippet of code:

function Animal() {

}

Animal.prototype.run = function() {
    (function() {
        console.log(this);
    })();
}

var g = new Animal();
g.run();

It logs Window and this makes sense, but in the following piece, it outputs undefined!!!:

class Animal {
    run() {

        (function() {
            console.log(this);
        })();

    }
}

var g = new Animal();
g.run();

This is the same code but using class syntax, what is different in class syntax that made this equal undefined?

Ayman El Temsahi
  • 2,600
  • 2
  • 17
  • 27

0 Answers0