Showing posts with label scope. Show all posts
Showing posts with label scope. Show all posts

Thursday, 25 May 2017

"this" in JavaScript/TypeScript

I have been struggling with using "this" in JavaScript, ever since I got into that area of programming.

There are lots of warnings on the web, where programmers who are used to a certain behaviour regarding "this" (Like me) can fall into this trap.

I recently found some really good resources that explain it.

There's one1 that explains it a little regarding "this" in JavaScript.

But as I have been writing in TypeScript, I was looking for an explanation that focuses on TypeScript and helps me find the best solution to deal with this. I found that one in [2].

For example

So I've got some code that could use a bit of a look-over.

Here's the troublesome bit.

TypeScript has an excellent Tutorial, which I've used time and again to write my things. One of the pages I've used is the explanation regarding HTTP which you can find at [3].

In it they mention a "handleError" method, which can handle HTTP errors of the PlayerService. Convenient, so I used it. It works.

Next, I wished for the handleError method in the PlayerService that takes care of HTTP connections to notify the ErrorsService. So naturally, I inject the ErrorsService into the PlayerService.

Unfortunately, in the handleError, the ErrorsService is 'undefined'. (See line 30 in the gist below)

It is explained in reference [2] why this is, but I like the following quote:
“The biggest red flag you can keep in mind is the use of a class method without immediately invoking it. Any time you see a class method being referenced without being invoked as part of that same expression, this might be incorrect.”
Now there are several solutions for this described in [2].

The solution below is what I came up with on my own, and I don't really like it, but it works.

Local Fat Arrow

I prefer the solution called the "Local Fat Arrow", which looks like this:
I love it!

References

[1] Mozilla Developer Network - javascript:this
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/this
[2] Github/Microsoft/TypeScript - 'this'in TypeScript
https://github.com/Microsoft/TypeScript/wiki/'this'-in-TypeScript
[3] ts GUIDE - HTTP CLIENT
https://angular.io/docs/ts/latest/guide/server-communication.html

Monday, 16 March 2015

Javascript "let" keyword vs "var" keyword

One of the differences between JavaScript and Java, is regarding variable scope.

In JavaScript there are two kinds of scope, global scope and function scope. Any local variable declared in a function, is visible everywhere in the function.

In the example above, both secondCarName and thirdCarName are local variables, only accessable within the function.

Note that, though the thirdCarName is declared inside an if-then block, this matters naught.

A gobal variable is visible everywhere. In the example above, this would be firstCarName.

Java

In contrast, in Java, scope can be within a method (sort of the equivalent of function), but also in block scope.

Therefore, netherlands is only available within the if statement.

let vs. var

Apparently, ECMAScript saw fit to provide JavaScript with a way to also support the Java-way of scope using the new let keyword.

But be wary, there will probably always be situations where Java and JavaScript will display different behaviour.

With let, it is possible to create a variable that obscures the variable in the higher block. This is in contrast with Java, where a error is thrown during compiling.

Disclaimer

My experience with JavaScript is spotty at best. The intricacies of scope in Java and JavaScript are more subtle than the isolated examples described here. And the new let keyword makes the scope in JavaScript even more subtle.

References

Stackoverflow - Javascript - “let” keyword vs “var” keyword
http://stackoverflow.com/questions/762011/javascript-let-keyword-vs-var-keyword
JavaScript|MDN - let keyword
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
Hangar.Runway 7 - The Javascript Guide to Objects, Functions, Scope, Prototypes and Closures
http://hangar.runway7.net/javascript/guide