Asynchronous callbacks in JavaScript
var Queue = (function () { "use strict"; function make() { var results = [], stack = [], queue = []; function buildControl(queue) { var obj = { next: function () { obj.next = function () {};...
View ArticleAutoscroll in Chrome (Linux)
2009/12/14 Update: AutoScroll has been submitted to Google Chrome Extensions. You can find the link at the bottom of this post. I recommend users install that one instead, so that you can receive...
View ArticleNegation in regular expressions
2009/09/07 Update:David Jones has posted a very simple solution to my contrived problem: /\b(?!foo\b)[a-zA-Z]+/g The above syntax does indeed solve the problem as laid out in this post. What I'm...
View ArticleSyntax Highlighting
In my previous post, I described the KAE.query.highlight plugin, which does syntax highlighting in JavaScript. I will now show you how I achieved this relatively easy task. Naturally the best way is...
View ArticleKAE.query.highlight
For a long while now I had been using the wonderful SyntaxHighlighter for syntax highlighting on this blog. I had a (possibly) odd requirement, however: I wanted to be able to syntax highlight code...
View ArticleTimer constructor.
"use strict"; var Timer = function (iter) { function manip(item) { return item; } this.average = function (func) { manip = func; }; this.results = function () { var i, length = this.length, times =...
View ArticleLambdas and closures.
Two of the smartest things about JavaScript are lambdas and closures, which were taken from Scheme. Why should you care about this? Because, using lambdas and closures, you can do things that would...
View ArticleName change to KAE Scripts.
We are now "KAE Scripts". There were various reasons for this: The previous name lacked identity.It also wasn't very descriptive at all.Besides, why bother having the name "KAE" if you don't use it?...
View ArticleHTTP.cookie
Chances are, at some point or other you'll have to work with cookies, the little pieces of data sent in HTTP requests. Unfortunately, the way to work with cookies in JavaScript is absolutely horrible....
View Articlearguments.callee
ECMAScript 5 strict mode has completely removed arguments.callee. Now, there have been very good arguments both for and againstarguments.callee, and I'm not going to go into them. However, what I will...
View ArticleGame of Life in JavaScript.
[LINK] Conway's Game of Life I have been fascinated by the Game of Life for some time now, and recently had the idea to make it in JavaScript. Although the idea is hardly new, I am hoping that my...
View ArticleCoord constructor.
In an earlier post, I mentioned that I never had need for a genuine hash. Although that is still technically correct, I did end up using bits and pieces of the Hash constructor to create a Coord...
View ArticleTiming in JavaScript.
2009/07/02 Update: I have created a far better version, which you can find here: [LINK] "use strict"; var getTime = function (func, length) { var i, start, end; length = length || 1; start = new...
View ArticleConstructors and "this".
2009/08/14 Update: It has come to my attention that there is a better way of doing this: var Point = function (x, y) { if (this === window) { return new Point(x, y); } this.x = x; this.y = y; };...
View ArticleJavaScript associative arrays.
JavaScript does not have a distinct "hash" type: all objects are hashes, including arrays, functions, etc. Due to this extreme flexibility, most people can get by without ever needing a so-called...
View ArticleThe && and || operators.
In JavaScript, the && and || operators are used as short-hands for conditionals. For instance: if (foo) { if (bar) { } } Can be written much more succinctly like this: if (foo && bar)...
View ArticleEssential JavaScript functions.
2009/07/22 Update: Added Object.keys. Removed String.prototype.*, alert, and waitUntil. "use strict"; if (typeof Object.create !== "function") { Object.create = function (o) { function F() {}...
View Articlewindow.waitUntil
2009/08/23 Update: This is only really useful if you need to do asynchronous calls 11 or more times. If you only need to make a few calls, it would be better to just use the (function () {}());...
View Articlewindow.alert
One of the problems with the alert() function is the inability to cancel it when placed within loops. You may find this function useful: "use strict"; var alert = function anon() { if (!anon.stop) {...
View ArticleReflecting a vector.
Let us assume: angle = the angle of the incoming vector. wall = the angle of the "wall". The formula for the reflection of an incoming vector in degrees is: angle - (2 * (angle + (90 - wall))) There is...
View Article