> When you call a function, whatever is left of the dot is passed as the `this` argument
The analogy is pretty good - you can't pass an argument to a function until you actually call it. In your example, the function hasn't been called yet, so no argument has been passed.
foo.doStuff // <-- syntax to access a field member
foo.doStuff() // syntax to call a field member with a `this` argument passed left of the dot.
Furthermore, on the receiving side, the setTimeout function is seeing: function setTimeout(fn, delay) {
// just fn, but not the argument.
}
Which makes it clear that you can no longer call that function with the proper `this` argument.My point was that `this` is only as confusing as you make it, and there is a simple and straightforward way to teach it (function argument). If you do that, the dozens of seemingly special cases go down to just a couple.
Its unfortunate we don't yet have the bind operator https://github.com/tc39/proposal-bind-operator
If we had it, the explanation could be made even clearer. You would be able to call free-standing functions with a `this` argument left of the double colon, i.e. `thisArgument::freeStandingFunction()` - then the dot method call syntax `thisArgument.method()` could be explained as a syntax sugar for `thisArgument::(thisArgument.method)()` - a syntax sugar to at the same time access a function attached as a member field of an object and call it with that object as the `this` argument.
The bind operator would make all those other `this` cases seem less special too. Of course you can call various bits of code with whatever `this` argument you want. The event handler code? `someElement::eventHandlerCode(event)`. A constructor? `Object.create(constructor.prototype)::constructor()`. Nothing special here, just a function argument, can have different values in different functions, depending what was passed as `this`.
Its bizarre that this operator got stuck in stage 0 and is being subsumed by the pipeline operator (a terrible match for JavaScript). Looks like TC39 isn't interested in untangling the mess.