A
A
AlexNew222022-04-21 23:44:56
JavaScript
AlexNew22, 2022-04-21 23:44:56

Why do object and class have different this?

Why does this refer to the class in the first case, and to Windows in the second?
And how is this looked up when calling functions?

class Test {
    static t = this
}
Test.t

const obj = { t: this }
obj.t

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Programmer_Man, 2022-04-22
@Programmer_Man

Try creating a function on the object that will return this:

const obj = {
    GetThis(){
        return this;
    }
}

console.log(obj.GetThis());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question