Posts filed under the 'JavaScriptLessons' category


Thinking functionally in JavaScript (part two)

Last time I took a quick look at why JavaScript can be used in a functional manner, primarily though the use of higher-order functions. Another way of putting this is that functions are objects in JavaScript, in the sense that they can be passed to and returned from other functions. And once you say “objects” as a programmer, you start thinking about things like composition, state, inheritance, and so on. Enter the well-known SOLID principles for object-oriented programming, but how are they applicable...

READ MORE

Thinking functionally in JavaScript (part one)

Over the Christmas break, when traditionally things are a little quieter at work, I do a bit of research into topics that interest me and that might have some bearing on our future products. This year was no exception and I decided to investigate React , Facebook’s library (framework?) for building user interfaces for the web. It’s a fascinating library to be sure (and I’ll talk more on it in another post), but there was one paradigm it uses which I haven’t really talked about before: immutability...

READ MORE

JSLint and recursive functions in JavaScript

The one issue that’s weird about JavaScript that I’ve found is that it has no compiler: the first time you find a bug is when you run your code, not when you compile it. So, since I’m adamant about writing “good” JavaScript code as I write it, I’ve installed a couple of JSLint plug-ins, one for Visual Studio and one for Sublime Text . JSLint is Douglas Crockford’s linter for JavaScript and, although utterly (perhaps barmily?) strict in certain areas, is the one I’ve settled on for checking/validating...

READ MORE

Declare that function! But, how?

So, I read a blog post about declaring functions in JavaScript recently. It was just about technically correct as far as it went, but, ow, the way it was written hurt (missing semicolons, invalid code samples, etc). I’ll decline to link to it, thanks very much, but instead I’ll bring out its main points together with a recommendation or two (that, in fact, the original blog post did not). There are two ways to declare a function in JavaScript, although we can spin ’em out a bit...

READ MORE

Globals, IIFEs, this, and strict mode

It started like this: I was reading some JavaScript code, written – he says charitably – some years ago. Much simplified, it looked something like this, with all identifiers renamed to protect the guilty: timer = {}; timer.timerID = null ; timer.cancel = function () { if (timer.timerID !== null ) { clearTimeout(timer.timerID); timer.timerID = null ; } }; timer.start = function (delay, onDone) { timer.cancel(); timer.timerID = setTimeout(onDone, delay); }; Colored light Let’s just...

READ MORE

Adventures with JSONP and jQuery

This whole thing started out as a nice-to-have. I have a blog (you’re reading it). I have a URL shortener (jmbk.nl). They are separate apps on separate domains. When I publish a post here, I diligently create the short URL for it manually in order to publish that short URL on social sites (the URL shortener has some minimalist stats associated with each short URL; so minimal, it’s only a count of the number of times it was used). Yeah, I know, silly, huh: why can’t each post generate its own short...

READ MORE

Awesome use of AND operator

I was reading some JavaScript code the other day, because, you know, reading someone else’s code gives you insights and inspirations to improve your own, when I came across this function to calculate the maximum element in an array: function max(a) { for ( var i = a.length, maxValue = a[0]; i--; ) { a[i] > maxValue && (maxValue = a[i]); } return maxValue; } Taking it slowly, first of all, check out the for loop. For comparison, here’s how I’d write it myself: var maxValue = a[0]; for ...

READ MORE

JavaScript for C# developers: currying

There’s a concept in functional programming called currying which gives you the ability to create one function from another, and have the created function call the original but with some arguments prefilled in. To be ultra-rigorous, we assume the original function has n arguments, and currying produces a chain of n functions each taking one argument to produce the same results as the original. We, however, will skip that rigorous definition since it’s too constricting for our discussion here. Instead ...

READ MORE

JavaScript for C# developers: coercion

In C#, we have implicit and explicit conversions. In both cases the idea is that we, the readers of the code, are not surprised by any conversions that happen. That’s why we can freely intermix int s with float s in a floating point calculation and everything turns out just fine. The int s are implicitly converted to float s (there’s no data loss) and the calculation comes out right. However, when there’s a possibility of some kind of data loss (say, converting a ulong to a long variable) you have...

READ MORE

JavaScript for C# developers: the Module pattern (part 4) – debugging

(For background, please check out parts 1 , 2 , and 3 before reading this post.) One of the problems about private local variables inside closures is that you can’t see them. Well, duh, you might say, that’s what private means after all. Point taken, but there’s one scenario where it would be really nice to be able to check those enclosed (enclosured?) variables: debugging. Now, I’m not going to get into the whole argument here about debugging versus testing. I’m of the firm belief that you should...

READ MORE