seanbunderwood
2021-12-4 17:28:23

Bringing back the discussion of old programming books, I think I may need to buy this gem.


soegaard2
2021-12-4 19:00:53

JavaScript has “interesting” scope rules.


massung
2021-12-4 19:10:56

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));


soegaard2
2021-12-4 19:12:23

Sounds like dynamic scoping to me.


massung
2021-12-4 19:12:37

But only for a single variable. :slightly_smiling_face:


soegaard2
2021-12-4 19:12:42

True.


soegaard2
2021-12-4 19:17:21

Hmm. Does super work the same way?


samth
2021-12-4 21:02:02

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


soegaard2
2021-12-4 21:05:48

I have been reading up on the rules today. I found this overview:


soegaard2
2021-12-4 21:06:54

But all I really needed to know was that this is in scope everywhere.


soegaard2
2021-12-4 21:07:44

And that this is a keyword, so it can’t be used as variable names etc.


spdegabrielle
2021-12-4 23:09:02

You MUST


seanbunderwood
2021-12-4 23:30:10

I DID


seanbunderwood
2021-12-4 23:30:42

It has a whole chapter on issues specific to Hollerith cards.


jcoo092
2021-12-4 23:51:50

Hey, last I knew there was extremely good money to be made in COBOL.


rokitna
2021-12-5 04:17:51

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) {...}


rokitna
2021-12-5 04:20:27

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


rokitna
2021-12-5 04:23:48

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


rokitna
2021-12-5 04:46:37

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.


seanbunderwood
2021-12-5 04:52:33

Hollerith cards, not so much.