Loading topic
this does not work that way. Its value is set fresh each time a function runs, and it depends on how that particular call was made6 min readlet, const, arrow functions, and modules. The previous chapter showed that this is set by how a function is called. This chapter covers code that runs outside any function, 7 min readthis depends on how a function is called. This chapter covers the simplest call form, the plain function call, and the rule that app6 min readf(), where no object is involved and this falls back to undefined or the global object. Most real code calls functions as methods, as in user.save() or list.push(x). This chapter explains the rule for those calls, which is called **6 min readthis (default binding, sloppy vs strict mode) and how method calls get theirs (implicit binding through the object before the dot). It also assumes you are comfortable with functions as values, closures, and destructuring. The questi6 min readthis when you pull them off an object or pass them as callbacks. This chapter covers the most direct fix: telling the function exactly what this should be when you call it. Every regular function inherits two methods from Function.prototype fo5 min readthis, and that you have used call and apply to pass a this value for a single call. It also assumes you are comfortable with closures, because bind is easiest to understand as a closure that remembers a this value.6 min readthis has come from the call site in one of three ways. It was the default value in a plain call, the object before the dot in a method call, or a value you passed in with call, apply, or bind. The new keyword is the fourth rule. It does not reuse an existing object for this. It c5 min readcall and apply, hard binding with bind, and new binding. Real code often triggers more than one rule in the same call. A bound function can be stored on an 6 min readthis from the call site. The same function can see a different this each time, depending on whether it was called as a method, through call, with new, or as a plain function. Arrow functions work differently. They never get a this of6 min readcall/apply/bind, new, and lexical this in arrow functions. You should also be comfortable with class syntax and extends. Classes add no new binding rule. They combine t6 min readnew), how bind creates a hard-bound function, and how arrow functions take this from the scope around them. In this chapter those rules meet code you did not write, such as array methods, timers, pro6 min read