Loading topic
this (or self) always refers to the object a method belongs to. JavaScript works differently. The value of this inside a function is set each time the function is called, and it depends on how the call is written. Where the function was defined and which obj3 min readvar, let, and const. Before looking at functions, it helps to know what this is when no function is involved at all, at the top level of a file. The answer de3 min read'use strict' directive before. It covers the simplest call form in JavaScript: calling a function by name, with no object in front of it and no new. This is the default binding rule. It 3 min readobj.method() sets this to obj. It also assumes you are comfortable with function references, callbacks, and strict mode's default binding, where a plain call leaves this as undefined.3 min readthis, and why a method can lose its this when you pull it off its object. In those cases the engine picks this for you based on the call site. call and apply let you pick it yourself.3 min readthis and why extracting a method loses it. It also assumes you have used call and apply. Those two methods set this for a single call. bind sets it permanently: it returns a new function whose this stays the same no matter how that funct2 min readthis, and how bind returns a function with a fixed this. Here you will see the fourth way a call can set this: calling a function with new.2 min readcall, apply, and bind, and new binding. Sometimes more than one rule seems to apply to the same call. JavaScript settles these cases wit2 min readnew binding. It also assumes you understand closures, because arrow functions use the same mechanism for this.3 min readbind, new, and lexical this in arrow functions. Here we apply them to class syntax, where the rules behave the same way but some defaults are different.3 min readcall/apply, hard binding with bind, and lexical this in arrow functions. Here you apply them to the most common place this goes wrong, which is functions you hand to someone else to call.2 min read