Bringing back the discussion of old programming books, I think I may need to buy this gem.
JavaScript has “interesting” scope rules.
Not scoping, but function context. this is special and is bound to the context object of the function. For example:
bike.bind({name: "foo"})(); // "foo" Where confusion happens is with the => shorthand, which implicitly binds the context of the anonymous function. This is why you see a lot of JS code that looks like this:
let that = this;
// use `that` instead of `this` because `this` isn't bound to what you think it is...
array.map(x => that.doSomething(x));
Sounds like dynamic scoping to me.
But only for a single variable. :slightly_smiling_face:
True.
Hmm. Does super work the same way?
What’s happening here is that 1. if you don’t have a receiver then this is bound to the global object, and 2. top level bindings like name are also properties of the global object
I have been reading up on the rules today. I found this overview:
But all I really needed to know was that this is in scope everywhere.
And that this is a keyword, so it can’t be used as variable names etc.
You MUST
I DID
It has a whole chapter on issues specific to Hollerith cards.
Hey, last I knew there was extremely good money to be made in COBOL.
this and arguments are essentially lexically scoped anaphoric variables. The syntax function (a, b, c) {...} is basically sugar for a hypothetical syntax function_behind_the_scenes (this, arguments, a, b, c) {...}
obj1.bike() accesses the bike property of obj1 and then calls it, passing in obj1 as the this argument, and passing in 0 positional arguments
if your code starts with "use strict";, then bike() passes in null as the this argument. Otherwise, it passes in the global object as the this argument
If JavaScript had made it so function calls passed in the caller’s value of this as the this argument by default, then this would often propagate deeply across multiple function calls, and I think it would make sense to call this dynamically scoped. But it doesn’t quite do that.
Hollerith cards, not so much.