I have found weird thing with this JavaScript code.
class Person {
    constructor(name) {
        this.name = name;
    }
    speakName() {
        console.log(this.name);
    }
}
var person = new Person("John");
person.speakName(); // John
var speakName = person.speakName;
speakName();        // Error
I made a object named person from Person class. Invoking internal methods directly works fine, however when I reassign the speakName to global variable var speakName, it gives me this exception:
Cannot read property 'name' of undefined
So I thought that this of reassigned var speakName refers global object(global in Node.js, window in Browser JavaScript), however it wasn't both.
class Person {
    constructor(name) {
        this.name = name;
    }
    speakName() {
        // console.log(this.name);
        if(typeof global !== "undefined") {
            console.log(this == global);    // false
        }
        if(typeof window !== "undefined") {
            console.log(this == window);    // false
        }
    }
}
So, what exactly "this" points to? I thought that it was global object, but it seems it's not. Could anyone can explain this?
 
    