What is "this"?
Learn JS Fundamentals to jQuery & Pure DOM Scripting
The four rules for determining the call context (
> in javascript functions don't know where they live, they only know how they are called
therefore `this` will determined by the call site (how the function is called)
(except for the ES6 Class syntax)
> how the function is what matters not where it is defined
The four rules for determining the call context (this
)
- the [[Call]] operator ->
this
will be thewindow
(if none of the other rules apply) - Dot [[Call]] operator (
foo.bar()
) ->this
will be set to the left side of the operator new
keyword (varfoo = new bar();
) ->this
will be the new objectfoo
call
orapply
(foo.dosomething.call(bar, "hello");
) -> will be set to the first parameterbar