Posts tagged with 'functions'


Thinking functionally in JavaScript – a fun interlude

I’ve been talking about functional JavaScript for a few posts, but, to be honest, it’s nice to put the theory aside and just practice thinking and writing functionally. With that in mind, let see what we can do about fixing some “copy-n-paste” code. I bought a theme for this site a month or so back – you’re looking at it. As part of the theme, you get some HTML showing what various types of pages look like, the CSS to render it all, and some JavaScript. Usually the HTML/CSS is fine, but then I take...

READ MORE

Thinking functionally in JavaScript (part three)

In continuing this series of posts about functional JavaScript ( one , two ), I whimsically wondered if we could apply the SOLID principles of object-oriented programming . We took a look at S last time (the Single Responsibility Principle), and were fairly successful. The principle I introduced there was not only that the functions we write should do one thing and do it well. If we can embrace global immutability, so much the better (in other words, the function should not have side effects ). Small...

READ MORE

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

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

JavaScript for C# developers: calling functions and the ‘this’ variable

I can’t believe that I haven’t posted an article on how to call functions in JavaScript and what this gets set to for each of the various invocation patterns. It’s one of those things that catches people out periodically, so it’s well worth discussing at length. There are four different ways to call or invoke a JavaScript function: method invocation , function invocation , constructor invocation , and apply invocation . Each of these invocation patterns results in this being set to a different object...

READ MORE

JavaScript for C# programmers: wrapping an existing function to add extra functionality

I was answering a JavaScript question on stackoverflow when a common usage scenario presented itself, one with a subtle gotcha that could catch you out. Perfect for a quick blog post. (Note: you could also view this post as an adjunct to my popular JavaScript callback posts ( I , II , III ).) My solution to the problem involved adding some extra precondition code to an existing event handler. The event was the plotclick event of the jQuery Flot charting library , so the original event handler code...

READ MORE

JavaScript for C# programmers: Function basics and scope

Another post in the occasional series on programming in JavaScript from a C# developer's viewpoint. This episode: the basics of functions in JavaScript, especially with regard to scope. All right, here we are, finally. Writing some real executable code. Before we do, however, I have some very important information, and I need to make it a header, so you remember: Functions are objects This one concept, I believe, more than any other you've met so far, is the defining difference between C# and JavaScript...

READ MORE