Posts tagged with 'callback'


The PacMan problem

I came across this mathematical problem the other day: Consider n points on a circle, labeled clockwise from 0 to n -1. Initially PacMan begins at 0 and there is a dot at each of the remaining n -1 points. PacMan takes a random walk around the circle; at each step, it moves with probability 1/2 to one neighbor and with probability 1/2 to the other neighbor. (Note that points 0 and n -1 are neighbors.) The first time PacMan visits any point it eats the dot that is there. Which dot is most likely to...

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# developers: callbacks (part III)

In the previous two installments ( one , two ) we explored the use of callbacks through creating a mapp function for arrays (so called because the latest JavaScripts have a native map method already), and through creating a mapAsync function where the work is done asynchronously rather than serially. The reason for this was so that we could avoid triggering the browser’s “script running a long time” warning and, also, more importantly, provide the user with a responsive UI. photo © 2007 sputnik ...

READ MORE

JavaScript for C# developers: callbacks (part II)

Last time , we wrote a map method for arrays (which I had to call mapp , so that we didn’t clash with the native version present in many browsers). To use the map method, you have to provide a callback function that would be called for every element in the array. The first change I want to make today is to allow for missing elements in the array. In that case, the callback shouldn’t be called. Here’s the obvious way of implementing this change: Array.prototype.mapp = function (process...

READ MORE

JavaScript for C# developers: callbacks (part I)

As I’ve said pretty much from the very start of this series: functions are objects. You can pass them around in variables, pass them into functions as parameters, return them from functions, the whole nine yards. When you pass a function into another where it will be called, it’s generally known as a callback . (Note: as usual I’m using Firebug in Firefox to run these examples. All browsers have similar development tools built-in or that are available as a download.) Here’s a simple example. There...

READ MORE