I'm trying to learn the 'this' concept in Javascript.
I have this simple code snippet:
sayHi = function(){
console.log('hi');
console.log(this);
}
class Test{
constructor(){
console.log(this);
}
instanceFunction(){
sayHi();
}
}
let ob = new Test();
ob.instanceFunction();
Output is:
Test {}
hi
[Global Object in Node]
May be I'm missing something basic, but I was expecting the third line in the output to be Test {}
instead of [Global Object in Node]
. How come the third line is the Global object, when invoked with an instance of Test
class ?