<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6133409975588434312</id><updated>2011-08-05T18:39:34.734-07:00</updated><category term='Extensions'/><category term='Mathematics'/><category term='JavaScript'/><category term='Chrome'/><category term='Game of Life'/><title type='text'>KAE Scripts</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>21</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-4600277368467992239</id><published>2010-02-25T01:35:00.000-08:00</published><updated>2011-04-15T00:38:17.982-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>Asynchronous callbacks in JavaScript</title><content type='html'>&lt;pre class=":js" data-options="collapsed: true"&gt;
var Queue = (function () {
    "use strict";

    function make() {
        var results = [],
            stack = [],
            queue = [];

        function buildControl(queue) {
            var obj = {
                next: function () {
                    obj.next = function () {};
                    stack.shift();

                    var first = stack[0];
                    if (first) {
                        first(buildControl(queue));
                    } else {
                        queue();
                    }
                }
            };
            return obj;
        }

        var obj = {
            make: make,

            sync: function (func) {
                if (typeof func === "function") {
                    stack.push(func);

                    if (stack.length === 1) {
                        func(buildControl(obj.async()));
                    }
                }
            },

            async: function (func) {
                var index = queue.push(func) - 1;

                return function () {
                    if (index === null) {
                        return;
                    }

                    results[index] = Array.prototype.slice.call(arguments);
                    index = null;

                    for (var i = 0; i &lt; queue.length; i += 1) {
                        if (results[i]) {
                            if (typeof queue[i] === "function") {
                                queue[i].apply(null, results[i]);
                                delete queue[i];
                            }
                        } else {
                            return;
                        }
                    }

                    queue.length = results.length = 0;
                };
            },

            run: function (func) {
                return obj.async(func)();
            }
        };
        return obj;
    }

    return make();
}());
&lt;/pre&gt;
&lt;p&gt;
JavaScript is normally synchronous: it executes each line in sequence. Certain functions, however, operate asynchronously: they do not halt execution, yet you need a way to get the results at a later time. To deal with these situations, you usually pass in a callback function that is run when the asynchronous call completes.
&lt;/p&gt;&lt;p&gt;
This works rather well, until you try to mix asynchronous and synchronous code together. Consider, for example, that you are trying to make multiple calls using &lt;code class=":js"&gt;XMLHttpRequest&lt;/code&gt;. The requests themselves are asynchronous, but you want them to behave synchronously.
&lt;/p&gt;&lt;p&gt;
Here's an example to illustrate the problem better. Assume the function &lt;code class=":js"&gt;getURL&lt;/code&gt; asynchronously loads a URL and returns it's contents:
&lt;/p&gt;
&lt;pre class=":js"&gt;
getURL("foo", function (data) {
    // do something with data here

    getURL("bar", function (data) {
        // do something with data here

        getURL("qux", function (data) {
            // do something with data here
        });
    });
});
&lt;/pre&gt;
&lt;p&gt;
In order to do this, you normally need to stack each asynchronous call inside the callback of the previous call! Being able to write them linearly, however, can be useful. Using this library, you can now do this:
&lt;/p&gt;
&lt;pre class=":js"&gt;
Queue.sync(function (queue) {
    getURL("foo", function (data) {
        // do something with data here
        queue.next();
    });
});

Queue.sync(function (queue) {
    getURL("bar", function (data) {
        // do something with data here
        queue.next();
    });
});

Queue.sync(function (queue) {
    getURL("qux", function (data) {
        // do something with data here
        queue.next();
    });
});
&lt;/pre&gt;
&lt;p&gt;
As you can see, we're adding functions onto a queue. When an asynchronous call within the queue is done, it calls &lt;code class=":js"&gt;queue.next()&lt;/code&gt;, which then executes the next function in the queue.
&lt;/p&gt;&lt;p&gt;
An even better example is being able to synchronously execute calls within a loop:
&lt;/p&gt;
&lt;pre class=":js"&gt;
["foo", "bar", "qux"].forEach(function (url) {
    Queue.sync(function (queue) {
        getURL(url, function (data) {
            // do something with data here
            queue.next();
        });
    });
});
&lt;/pre&gt;
&lt;p&gt;
The above does the same thing as before, but uses a loop to generate the three calls. This makes it trivial to add new calls: just add a string to the array. This also opens up the possibility of dynamically creating an array, and then looping over it and executing asynchronous callbacks in order.
&lt;/p&gt;&lt;p&gt;
There is one problem with the previous three examples. Although each call to &lt;code class=":js"&gt;getURL&lt;/code&gt; is asynchronous, it waits before executing the next call. It would be better to execute all the calls in parallel, but display the results sequentially. Here's an example of how to do that:
&lt;/p&gt;
&lt;pre class=":js"&gt;
["foo", "bar", "qux"].forEach(function (url) {
    getURL(url, Queue.async(function (data) {
        // do something with data here
    }));
});
&lt;/pre&gt;
&lt;p&gt;
This should return the same results as the previous examples, but performs faster because the three calls are executing in parallel. To be more specific, the results are limited by the speed of the slowest call, rather than the sum of all the calls.
&lt;/p&gt;&lt;p&gt;
So how does it work? &lt;code class=":js"&gt;Queue.async&lt;/code&gt; returns a function. When that function is called, it executes (in sequential order) the results that have been found so far, making sure not to execute the same result twice. When it finds a call that doesn't have a result yet, it stops.
&lt;/p&gt;&lt;p&gt;
Where I've found this most useful is when creating extensions for Google Chrome. In the Chrome extension system, almost all of the API functions are asynchronous: you have to pass in a callback if you want to retrieve the results of the function.
&lt;/p&gt;&lt;p&gt;
Unfortunately, stacking callbacks inside of callbacks reduces readability, especially if they are deeply nested. Using the above construct, you can program in a linear style, thus avoiding race conditions while retaining the benefits of asynchronous code.
&lt;/p&gt;&lt;p&gt;
Also, if you want to push a synchronous function onto the queue, you can use &lt;code class=":js"&gt;Queue.run&lt;/code&gt;:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var results = {};

["foo", "bar", "qux"].forEach(function (url) {
    getURL(url, Queue.async(function (data) {
        results[url] = data;
    }));
});

Queue.run(function () {
    console.log(results);
});
&lt;/pre&gt;
&lt;p&gt;
The above executes three asynchronous calls in parallel, and when they're all done, it runs the function passed to &lt;code class=":js"&gt;Queue.run&lt;/code&gt;, which then logs the results.
&lt;/p&gt;&lt;p&gt;
Here's how you might do it if you didn't have &lt;code class=":js"&gt;Queue&lt;/code&gt;:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var results = {};

function end() {
    console.log(results);
}

var index = 3;

["foo", "bar", "qux"].forEach(function (url) {
    getURL(url, function (data) {
        results[url] = data;

        index -= 1;
        if (index === 0) {
            end();
        }
    });
});
&lt;/pre&gt;
&lt;p&gt;
Lastly, you can use &lt;code class=":js"&gt;Queue.make&lt;/code&gt; to create multiple queues. Ordinarily, if you use &lt;code class=":js"&gt;Queue.sync&lt;/code&gt;, only one function can execute at a time. But by using multiple queues, you can have one function executing per queue. This also lets you nest calls to &lt;code class=":js"&gt;Queue.sync&lt;/code&gt;:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var one = Queue.make(),
    two = Queue.make();

[".gif", ".jpg", ".png"].forEach(function (suffix) {
    one.sync(function (queue) {

        ["foo", "bar"].forEach(function (name) {
            two.sync(function (queue) {

                getURL(name + suffix, function (data) {
                    // do something with data here

                    queue.next();
                });
            });
        });

        two.run(queue.next);
    });
});
&lt;/pre&gt;
&lt;p&gt;
The above synchronously executes calls to &lt;code class=":js"&gt;getURL&lt;/code&gt; in this order:&lt;br /&gt;
&lt;code class=":js"&gt;"foo.gif"&lt;/code&gt;, &lt;code class=":js"&gt;"bar.gif"&lt;/code&gt;,&lt;br /&gt;
&lt;code class=":js"&gt;"foo.jpg"&lt;/code&gt;, &lt;code class=":js"&gt;"bar.jpg"&lt;/code&gt;,&lt;br /&gt;
&lt;code class=":js"&gt;"foo.png"&lt;/code&gt;, &lt;code class=":js"&gt;"bar.png"&lt;/code&gt;
&lt;/p&gt;&lt;p&gt;
Why do you need two queues? Here is what the queue would look like if you used only one queue:
&lt;/p&gt;&lt;p&gt;
&lt;code&gt;&lt;b&gt;.gif&lt;/b&gt; &lt;i&gt;foo bar&lt;/i&gt; &lt;b&gt;.jpg .png&lt;/b&gt; &lt;i&gt;foo bar foo bar&lt;/i&gt;&lt;/code&gt;
&lt;/p&gt;&lt;p&gt;
But by using two queues, it looks like this, which is what you want:
&lt;/p&gt;&lt;p&gt;
&lt;code&gt;&lt;b&gt;.gif&lt;/b&gt; &lt;i&gt;foo bar&lt;/i&gt; &lt;b&gt;.jpg&lt;/b&gt; &lt;i&gt;foo bar&lt;/i&gt; &lt;b&gt;.png&lt;/b&gt; &lt;i&gt;foo bar&lt;/i&gt;&lt;/code&gt;
&lt;/p&gt;&lt;p&gt;
In this particular case, we could have used a single queue and it might have worked okay, but in a different case two queues might be necessary to avoid this issue.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-4600277368467992239?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/4600277368467992239/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2010/02/asynchronous-callbacks-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/4600277368467992239'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/4600277368467992239'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2010/02/asynchronous-callbacks-in-javascript.html' title='Asynchronous callbacks in JavaScript'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-3673432362825248517</id><published>2009-12-12T06:31:00.000-08:00</published><updated>2009-12-14T09:12:06.506-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='Extensions'/><category scheme='http://www.blogger.com/atom/ns#' term='Chrome'/><title type='text'>Autoscroll in Chrome (Linux)</title><content type='html'>&lt;p&gt;
&lt;strong&gt;2009/12/14 Update:&lt;/strong&gt;&lt;br&gt;
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 automatic updates. This also fixes a bug where it scrolled on password fields.
&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;
Almost 5 months ago, &lt;a href="http://code.google.com/p/chromium/issues/detail?id=17689"&gt;a bug&lt;/a&gt; was filed on the Chromium issue tracker, asking that autoscroll be implemented in Linux.
&lt;/p&gt;&lt;p&gt;
Seeing as how it won't be implemented anytime soon, I created an extension to add this feature in. Here were my design goals:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sticky and non-sticky scroll&lt;/li&gt;
&lt;li&gt;Ability to customize aspects (such as scroll speed)&lt;/li&gt;
&lt;li&gt;Fluid and natural behavior&lt;/li&gt;
&lt;li&gt;Try to follow what Firefox (and other browsers) do&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
For the most part, I believe I fulfilled those goals. Try it out!
&lt;/p&gt;&lt;p&gt;
&lt;a href="https://chrome.google.com/extensions/detail/occjjkgifpmdgodlplnacmkejpdionan"&gt;[LINK]&lt;/a&gt; AutoScroll Extension for Chrome.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-3673432362825248517?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/3673432362825248517/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/12/autoscroll-in-chrome-linux.html#comment-form' title='35 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/3673432362825248517'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/3673432362825248517'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/12/autoscroll-in-chrome-linux.html' title='Autoscroll in Chrome (Linux)'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>35</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-851273909503024524</id><published>2009-08-23T22:14:00.000-07:00</published><updated>2009-09-07T03:15:53.606-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>Negation in regular expressions</title><content type='html'>&lt;p&gt;
&lt;strong&gt;2009/09/07 Update:&lt;/strong&gt;&lt;br&gt;
&lt;a href="http://drj11.wordpress.com/"&gt;David Jones&lt;/a&gt; has posted a very simple solution to my contrived problem:
&lt;pre class=":js"&gt;/\b(?!foo\b)[a-zA-Z]+/g&lt;/pre&gt;
The above syntax does indeed solve the problem as laid out in this post. What I'm curious about now is if there is any situation where my proposed syntax works, and the above does not.
&lt;/p&gt;&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;&lt;p&gt;
Regular expressions are highly useful things, with a variety of purposes. Nonetheless, I believe there is room to improve them. For instance, I have run across the following situation:
&lt;/p&gt;&lt;p&gt;
Let's suppose you want to find all the words in some text. This is rather simple:
&lt;pre class=":js"&gt;/[a-zA-Z]+/g&lt;/pre&gt;
But now let's suppose you want to find all the words, except the word "foo". This is incredibly more complicated. You can't use this construct:
&lt;pre class=":js"&gt;/[^f][^o][^o]/g&lt;/pre&gt;
For two primary reasons:
&lt;ol&gt;
&lt;li&gt;It would match non-letter characters, like punctuation.&lt;/li&gt;
&lt;li&gt;It wouldn't match the word "boo" or "sol", even though neither are equal to the word "foo".&lt;/li&gt;
&lt;/ol&gt;
&lt;/p&gt;&lt;p&gt;
Let's try to rectify the first point:
&lt;pre class=":js"&gt;/[a-eg-zA-EG-Z][a-np-zA-NP-Z]{2}[a-zA-Z]*/g&lt;/pre&gt;
Well, now it no longer picks up punctuation, but it has grown far more complicated! And it still won't match "boo" or "sol".
&lt;/p&gt;&lt;p&gt;
What is the correct solution, then? &lt;ins&gt;See the update above.&lt;/ins&gt; &lt;del&gt;There mostly isn't one. You could probably come up with an incredibly long and complicated regular expression, but it's just not worth it.&lt;/del&gt; So, I am proposing a new syntax for regular expressions:
&lt;pre class=":js"&gt;/[a-zA-Z]+(?^foo)/g&lt;/pre&gt;
&lt;/p&gt;&lt;p&gt;
The above will match every word, &lt;em&gt;except&lt;/em&gt; for the word "foo". However, it will match "boo", "sol", and "fool". On the other hand:
&lt;pre class=":js"&gt;/[a-zA-Z]+(?^foo.*)/g&lt;/pre&gt;
The above will match "boo" and "sol", but not "fool" or "foolhardy". This solves the problem of negation, within regular expressions.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-851273909503024524?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/851273909503024524/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/08/negation-in-regular-expressions.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/851273909503024524'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/851273909503024524'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/08/negation-in-regular-expressions.html' title='Negation in regular expressions'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-8667635544170444263</id><published>2009-07-15T01:04:00.000-07:00</published><updated>2009-08-23T21:54:40.598-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>Syntax Highlighting</title><content type='html'>&lt;p&gt;
In my &lt;a href="/2009/07/kaequeryhighlight.html"&gt;previous post&lt;/a&gt;, I described the &lt;code class=":js"&gt;KAE.query.highlight&lt;/code&gt; plugin, which does syntax highlighting in JavaScript. I will now show you how I achieved this relatively easy task.
&lt;/p&gt;&lt;p&gt;
Naturally the best way is to simply grab a copy of the &lt;a href="http://highlight.kae-query.googlecode.com/hg/KAE.query.highlight.src.js"&gt;source&lt;/a&gt;, and read the code (there are plenty of comments as well). However, only a small part of the code is doing the actual parsing, so it can be easy to get lost. This post will give a general overview of the steps required.
&lt;/p&gt;&lt;p&gt;
I'm going to assume that you want to allow users to create their own rules for parsing (brushes), and also supply their own themes (for colors, etc). Here are the basic steps:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Grab the brush and loop over it. Find all the matches in the source text, and store them in an array. You will need to know at least four things: the text, the CSS class, the start index, and the end index. These are all trivial to obtain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sort the array of matches by the start index, so the matches are in the correct order.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Loop over the matches. For each match, check to see if the current start index is greater than the previous end index. If so, add the match's text and apply the CSS class. If not, set the current match's end index to be the same as the previous match's end index.
&lt;/p&gt;&lt;p&gt;
This is important because you will have matches within matches. For instance, you might have a comment. You want it to ignore the matches that are inside of the comment, obviously. This also handles things like numbers inside of strings, etc.
&lt;/p&gt;&lt;p&gt;
Here is a diagram showing why this works:
&lt;img src="http://paaru.pbworks.com/f/HighlightParsing4-3.gif"&gt;
&lt;/p&gt;&lt;p&gt;
Let's examine it. The colored area shows the current match. The arrow shows the previous match's end index. One way to think of it is: if the arrow is to the right of the current match, we ignore the match and go to the next one.
&lt;/p&gt;&lt;p&gt;
The important thing is that if a match is inside of the previous match, we set the current match's end index to be the same as the previous match's end index. This allows us to recurse through the entire set, rather than stopping at the first.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;
Now, you want to use something like &lt;code class=":js"&gt;slice()&lt;/code&gt; to obtain the non-matching text in between the current match's end index, and the next match's start index. This handles things like this: &lt;code class=":js"&gt;Foo.bar.qux();&lt;/code&gt; Note how &lt;code class=":js"&gt;.bar.&lt;/code&gt; is not a match, but we want to include it, rather than leaving it out.
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
That's it! No, really, that's all the parsing that's required. &lt;code class=":js"&gt;KAE.query.highlight&lt;/code&gt;'s parsing code is only &lt;del title="Firefox 3.0 has issues. &amp;gt;:(" style="font-size:90%"&gt;30&lt;/del&gt; 38 lines! It also handles a few odd cases, and allows you to apply multiple brushes at the same time to the same element.
&lt;/p&gt;&lt;p&gt;
Just grab a copy of the &lt;a href="http://highlight.kae-query.googlecode.com/hg/KAE.query.highlight.src.js"&gt;source code&lt;/a&gt; and search for &lt;code class=":js"&gt;KAE.query.highlight.parser&lt;/code&gt;
&lt;/p&gt;&lt;p&gt;That function handles all the parsing. It should also be well commented, so hopefully you won't have any trouble understanding.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-8667635544170444263?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/8667635544170444263/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/07/syntax-highlighting.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/8667635544170444263'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/8667635544170444263'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/07/syntax-highlighting.html' title='Syntax Highlighting'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-5393663814649367290</id><published>2009-07-15T00:15:00.001-07:00</published><updated>2009-07-15T01:03:16.940-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>KAE.query.highlight</title><content type='html'>&lt;p&gt;
For a long while now I had been using the wonderful &lt;a href="http://alexgorbatchev.com/wiki/SyntaxHighlighter"&gt;SyntaxHighlighter&lt;/a&gt; for syntax highlighting on this blog.
&lt;/p&gt;&lt;p&gt;
I had a (possibly) odd requirement, however: I wanted to be able to syntax highlight code that was inline with non-code. Here's an example:
&lt;/p&gt;&lt;p&gt;
Blah blah blah &lt;code class=":js"&gt;var foo = "foo";&lt;/code&gt; blah blah blah.
&lt;/p&gt;&lt;p&gt;
SyntaxHighlighter won't let you do that. I created a quick stand-alone program that would allow for inline highlighting, but that added even more bloat to an already-big program. (SyntaxHighlighter is 1,984 lines long!)
&lt;/p&gt;&lt;p&gt;
I then decided to create my own syntax highlighter. One that was designed from the ground up to be very minimal and light-weight, and one that easily supported inline highlighting. The fruit of my labor is the &lt;code class=":js"&gt;KAE.query.highlight&lt;/code&gt; plugin.
&lt;/p&gt;&lt;p&gt;
Wait. Plugin? Not program? &lt;code class=":js"&gt;KAE.query.highlight&lt;/code&gt; relies on the &lt;code class=":js"&gt;KAE.query&lt;/code&gt; module in order to function. This may seem like a disadvantage at first, but let's look at the benefits:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Allows syntax highlighting on &lt;em&gt;any&lt;/em&gt; element, not just those with a special class.&lt;/li&gt;
&lt;li&gt;Provides a platform for building other useful plugins.&lt;/li&gt;
&lt;li&gt;&lt;code class=":js"&gt;KAE.query&lt;/code&gt; plugins can easily be ported to &lt;code class=":js"&gt;jQuery&lt;/code&gt;, due to the similarities in architecture.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
What is &lt;code class=":js"&gt;KAE.query&lt;/code&gt;, anyways? Think of it like &lt;code class=":js"&gt;jQuery&lt;/code&gt; without any features. You pass in a string, and &lt;code class=":js"&gt;KAE.query&lt;/code&gt; will return the DOM elements that match the string, just like &lt;code class=":js"&gt;jQuery&lt;/code&gt;. It supports a plugin system that is very similar to &lt;code class=":js"&gt;jQuery&lt;/code&gt;'s plugin system. Unlike &lt;code class=":js"&gt;jQuery&lt;/code&gt;, however, it lacks any useful methods: those must be provided with plugins. In essence, it is a light-weight stripped down version of &lt;code class=":js"&gt;jQuery&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
I don't harbor any ill feelings toward either &lt;code class=":js"&gt;jQuery&lt;/code&gt; or SyntaxHighlighter. However, both projects are targeted at something I don't need. I just wanted a simple way to extend the DOM NodeList, providing useful methods, like syntax highlighting.
&lt;/p&gt;&lt;p&gt;
Having said that, you might wonder why anybody would choose &lt;code class=":js"&gt;KAE.query.highlight&lt;/code&gt; over SyntaxHighlighter. Here are some reasons why I decided to create a new project:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Can be used to apply syntax highlighting with different options to different elements. For instance, you may want to apply different settings to a &lt;code class=":html"&gt;&amp;lt;code&gt;&lt;/code&gt; tag than you would want to apply to a &lt;code class=":html"&gt;&amp;lt;pre&gt;&lt;/code&gt; tag.&lt;/li&gt;
&lt;li&gt;Much smaller in terms of code size. This can make a big difference when viewers have not cached the JavaScript file.&lt;/li&gt;
&lt;li&gt;Uses a library-agnostic brush system that allows all JavaScript highlighters to use the same brushes.&lt;/li&gt;
&lt;li&gt;Non-destructive: &lt;code class=":js"&gt;KAE.query.highlight&lt;/code&gt; works on the original element, so any styles are preserved.&lt;/li&gt;
&lt;li&gt;Easily allows for syntax highlighting of inline elements.&lt;/li&gt;
&lt;li&gt;Much much faster.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
An interesting side effect of making it a &lt;code class=":js"&gt;KAE.query&lt;/code&gt; plugin is that it's backwards compatible with SyntaxHighlighter. You shouldn't need to change any HTML markup: it'll just work.
&lt;/p&gt;&lt;p&gt;
Some of these changes could be merged back into SyntaxHighlighter; in fact I encourage it. Some of the changes, however, may not be accepted.
&lt;/p&gt;&lt;p&gt;
Please do file any bugs or suggestions on the bug tracker listed below.
&lt;/p&gt;&lt;p&gt;
&lt;a href="http://code.google.com/p/kae-query/issues/list"&gt;[LINK]&lt;/a&gt; Bug Tracker
&lt;/p&gt;&lt;p&gt;
&lt;a href="http://code.google.com/p/kae-query/"&gt;[LINK]&lt;/a&gt; &lt;code class=":js"&gt;KAE.query&lt;/code&gt;&lt;br&gt;
&lt;a href="http://code.google.com/p/kae-query/wiki/PluginHighlight"&gt;[LINK]&lt;/a&gt; &lt;code class=":js"&gt;KAE.query.highlight&lt;/code&gt;
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-5393663814649367290?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/5393663814649367290/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/07/kaequeryhighlight.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/5393663814649367290'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/5393663814649367290'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/07/kaequeryhighlight.html' title='KAE.query.highlight'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-3762626429180783106</id><published>2009-07-02T21:59:00.000-07:00</published><updated>2009-08-23T21:54:46.085-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>Timer constructor.</title><content type='html'>&lt;pre class=":js"&gt;
"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 = [];
        for (i = 0; i &lt; length; i += 1) {
            times.push(manip(Timer.run(this[i], iter), iter));
        }
        return times;
    };
};
Timer.prototype = [];
Timer.run = function (func, length) {
    var i, start, end;
    length = length || 1;
    start = new Date();
    for (i = 0; i &lt; length; i += 1) {
        func();
    }
    end = new Date();
    return end - start;
};
&lt;/pre&gt;
&lt;p&gt;
In &lt;a href="/2009/05/timing-in-javascript.html"&gt;an earlier post&lt;/a&gt; I described a simple function for benchmark testing in JavaScript. There isn't anything wrong with this function (aside from it being a global), however it doesn't do very much.
&lt;/p&gt;&lt;p&gt;
Most of the time, when I want to do benchmarks, I'm comparing 2+ ways of doing the same thing. This allows me to pick whichever method is the fastest. It is possible with &lt;code class=":js"&gt;getTime&lt;/code&gt;, but it is cumbersome. I then set out to create the &lt;code class=":js"&gt;Timer&lt;/code&gt; constructor, to alleviate this problem.
&lt;/p&gt;&lt;p&gt;
You initialize it with &lt;code class=":js"&gt;new Timer()&lt;/code&gt;, and you can pass in an optional number, indicating the iterations. If you create it with &lt;code class=":js"&gt;new Timer(1000)&lt;/code&gt;, then every function will be called 1,000 times.
&lt;/p&gt;&lt;p&gt;
&lt;code class=":js"&gt;Timer&lt;/code&gt; is similar to an array of functions. You can add new functions with the &lt;code class=":js"&gt;push()&lt;/code&gt; method:
&lt;/p&gt;
&lt;p&gt;&lt;pre class=":js"&gt;
var timer = new Timer(100);
timer.push(function () {
    /* code goes here! */
});
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;
You can use &lt;code class=":js"&gt;push()&lt;/code&gt; to add as many functions as you like. In order to obtain the actual time it takes to run the functions, you call the &lt;code class=":js"&gt;results()&lt;/code&gt; method, which returns an array:
&lt;/p&gt;
&lt;p&gt;&lt;pre class=":js"&gt;
// An array of benchmarks:
timer.results();
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;
You can then call &lt;code class=":js"&gt;join()&lt;/code&gt; to display the array in various ways:
&lt;/p&gt;
&lt;p&gt;&lt;pre class=":js"&gt;
// Use custom separators:
timer.results().join("");
timer.results().join("\n");
timer.results().join(" + ");
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;
Lastly, there's the &lt;code class=":js"&gt;average()&lt;/code&gt; method, which allows you to manipulate the benchmark. For instance, to average the results based on the mean:
&lt;/p&gt;
&lt;p&gt;&lt;pre class=":js"&gt;
timer.average(function (item, iter) {
    return item / iter;
});
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;
You pass in a function, which is run after each benchmark has been computed. The first argument is how long it took to run the function, and the second argument is how many times the function was run (iterations). Whatever the function returns is used instead of the normal time.
&lt;/p&gt;&lt;p&gt;
Using these combined, you can create a simple script to compare two or more pieces of code, to determine which is fastest:
&lt;/p&gt;
&lt;p&gt;&lt;pre class=":js"&gt;
var timer = new Timer(10000);

//-- Begin benchmark functions
timer.push(function () {
    $("#test").css("backgroundColor", "black");
});

timer.push(function () {
    $("#test").css({
        backgroundColor: "black"
    });
});

timer.push(function () {
    $("#test").attr("style", "background-color: black;");
});
//-- End benchmark functions

timer.average(function (item, iter) {
    return item / iter;
});
alert(timer.results().join("\n"));
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;
The above runs three functions (that do the same thing), computes how long it takes to run them 10,000 times, averages the result, and lastly displays it. If you don't want it to average the result, simply leave out the &lt;code class=":js"&gt;timer.average()&lt;/code&gt; call.
&lt;/p&gt;&lt;p&gt;
Finally, you can still access the old &lt;code class=":js"&gt;getTime&lt;/code&gt; under the name &lt;code class=":js"&gt;Timer.run&lt;/code&gt;:
&lt;/p&gt;
&lt;p&gt;&lt;pre class=":js"&gt;
var time = Timer.run(function () {
    /* code goes here! */
}, 100000);
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://paaru.pbworks.com/f/Timer.src.js"&gt;[LINK]&lt;/a&gt; The source code.&lt;br&gt;
&lt;a href="http://paaru.pbworks.com/f/test.Timer.html"&gt;[LINK]&lt;/a&gt; The unit tests.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-3762626429180783106?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/3762626429180783106/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/07/timer-constructor.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/3762626429180783106'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/3762626429180783106'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/07/timer-constructor.html' title='Timer constructor.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-7190453637773393953</id><published>2009-06-26T13:43:00.000-07:00</published><updated>2009-07-11T00:33:52.223-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>Lambdas and closures.</title><content type='html'>&lt;p&gt;
Two of the smartest things about JavaScript are lambdas and closures, which were taken from Scheme.
&lt;/p&gt;&lt;p&gt;
Why should you care about this? Because, using lambdas and closures, you can do things that would either be impossible or difficult without. An example?
&lt;/p&gt;
&lt;pre class=":js"&gt;
Math.random = (function () {
    var random = Math.random;
    return function (min, max) {
        return max ? (random() * (max - min + 1) + min) : random();
    };
}());
&lt;/pre&gt;
&lt;p&gt;
What does this allow you to do? Well, hopefully you know that in JavaScript, you can use &lt;code class=":js"&gt;Math.random()&lt;/code&gt; to return a pseudo-random float between &lt;code class=":js"&gt;0.0&lt;/code&gt; and &lt;code class=":js"&gt;1.0&lt;/code&gt;. This is very useful, of course, as you can then manipulate that value in various ways.
&lt;/p&gt;&lt;p&gt;
Something I had need of is a function that will return a pseudo-random number within a range. In other words, I wanted to call &lt;code class=":js"&gt;Math.random(1, 6)&lt;/code&gt; to simulate a dice-roll (returning a number between 1 and 6).
&lt;/p&gt;&lt;p&gt;
"What? How?! You're overwriting Math.random, yet you still need to be able to call it!", you may be saying. That is all true, of course. Even though I have to overwrite &lt;code class=":js"&gt;Math.random&lt;/code&gt;, I still want access to the old version. This is possible (and easy) with closures.
&lt;/p&gt;&lt;p&gt;
First, I create an anonymous function (lambda) and execute it immediately. Inside I create a reference to &lt;code class=":js"&gt;Math.random&lt;/code&gt; in the variable &lt;code class=":js"&gt;random&lt;/code&gt;. I can now refer to the old &lt;code class=":js"&gt;Math.random&lt;/code&gt; within the anonymous function.
&lt;/p&gt;&lt;p&gt;
Because I'm assigning this to a variable, whatever the lambda returns will be put into &lt;code class=":js"&gt;Math.random&lt;/code&gt;. In this case, I return a function, which has access to the variable &lt;code class=":js"&gt;random&lt;/code&gt;. This is called a closure.
&lt;/p&gt;&lt;p&gt;
Now, within the returned function, I can use &lt;code class=":js"&gt;random&lt;/code&gt; to refer to the old &lt;code class=":js"&gt;Math.random&lt;/code&gt;, allowing me to perform all the wizardry I need. Aren't lambdas and closures wonderful?
&lt;/p&gt;&lt;p&gt;
Here's some examples of how to use it:
&lt;/p&gt;
&lt;pre class=":js"&gt;
// Returns a pseudo-random float between 0.0 and 1.0:
Math.random();

// Returns a pseudo-random float between 5.0 and 8.0:
Math.random(5, 8);

// Returns a pseudo-random int between 5 and 8:
Math.floor(Math.random(5, 8));
&lt;/pre&gt;
&lt;p&gt;
&lt;b&gt;Note:&lt;/b&gt; &amp;nbsp;&lt;code class=":js"&gt;Math.floor&lt;/code&gt; is significant. &lt;code class=":js"&gt;Math.round&lt;/code&gt; would produce incorrect numbers.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-7190453637773393953?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/7190453637773393953/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/06/lambdas-and-closures.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/7190453637773393953'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/7190453637773393953'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/06/lambdas-and-closures.html' title='Lambdas and closures.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-7900613117420607859</id><published>2009-06-11T13:55:00.000-07:00</published><updated>2009-06-11T13:59:21.366-07:00</updated><title type='text'>Name change to KAE Scripts.</title><content type='html'>&lt;p&gt;
We are now "KAE Scripts". There were various reasons for this:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The previous name lacked identity.&lt;/li&gt;
&lt;li&gt;It also wasn't very descriptive at all.&lt;/li&gt;
&lt;li&gt;Besides, why bother having the name "KAE" if you don't use it?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
This solves all of the above problems. "KAE" is used in the title, thus providing identity, and "Scripts" implies some sort of scripting language (like JavaScript), giving focus and context.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-7900613117420607859?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/7900613117420607859/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/06/name-change-to-kae-scripts.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/7900613117420607859'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/7900613117420607859'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/06/name-change-to-kae-scripts.html' title='Name change to KAE Scripts.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-3180840687650625902</id><published>2009-06-11T10:43:00.000-07:00</published><updated>2009-07-11T00:32:36.939-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>HTTP.cookie</title><content type='html'>&lt;p&gt;
Chances are, at some point or other you'll have to work with cookies, the little pieces of data sent in HTTP requests.
&lt;/p&gt;&lt;p&gt;
Unfortunately, the way to work with cookies in JavaScript is absolutely horrible. Compounding the problem, there isn't any standard easy way to get/set a cookie, so we end up with dozens of different functions, each trying to do exactly the same thing.
&lt;/p&gt;&lt;p&gt;
Even worse, a lot of these functions (though they work), are very verbose and inefficient, making them both unwieldy and slow. If that wasn't bad enough, they often pollute the global namespace (sometimes with 3 global functions!).
&lt;/p&gt;&lt;p&gt;
As awful as dealing with cookies is, I found myself needing to do this. I had some criteria that I wanted in a cookie function:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Support getting/setting/deleting cookies.&lt;/li&gt;
&lt;li&gt;Allow all the cookie settings, like path, domain, max-age, etc.&lt;/li&gt;
&lt;li&gt;Use the same function for everything.&lt;/li&gt;
&lt;li&gt;Lightweight in terms of code, and also very fast.&lt;/li&gt;
&lt;li&gt;Use at most one global variable, preferably none.&lt;/li&gt;
&lt;li&gt;Not tied to any libraries, making it easy to embed.&lt;/li&gt;
&lt;li&gt;Simple and intuitive to use.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Nothing fulfilled all the above requirements, so I implemented my own. It uses the &lt;code class=":js"&gt;HTTP&lt;/code&gt; global variable, in case somebody wants to add another method dealing with HTTP.
&lt;/p&gt;&lt;p&gt;
You use it like this:
&lt;/p&gt;
&lt;pre class=":js"&gt;
// Get a cookie:
HTTP.cookie("test");

// Set a cookie:
HTTP.cookie("test", "Hi!");

// Delete a cookie:
HTTP.cookie("test", null);
&lt;/pre&gt;
&lt;p&gt;
In addition, you can pass along a third parameter, which lets you set the special cookie settings:
&lt;/p&gt;
&lt;pre class=":js"&gt;
// All the settings at once:
HTTP.cookie("test", "Hi!", {
    path: "/",
    domain: "temp",
    maxAge: 31536000,
    expires: new Date(2010, 0, 0),
    secure: true
});
&lt;/pre&gt;
&lt;p&gt;
Every setting works as you would expect. As per the specification, &lt;code class=":js"&gt;expires&lt;/code&gt; should be a GMT-string. You can pass in a string directly, or a &lt;code class=":js"&gt;Date&lt;/code&gt; object, which will then be converted into a GMT-string.
&lt;/p&gt;&lt;p&gt;
&lt;strong&gt;NOTE:&lt;/strong&gt; If you set a cookie with path, domain, or secure, you must delete the cookie with the same settings. In other words: two cookies with the same name and value (but different settings) are considered as two different cookies.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://paaru.pbworks.com/f/HTTP.cookie.src.js"&gt;[LINK]&lt;/a&gt; The source code.&lt;br&gt;
&lt;a href="http://paaru.pbworks.com/f/test.HTTP.cookie.html"&gt;[LINK]&lt;/a&gt; The unit tests.
&lt;/p&gt;&lt;p&gt;
&lt;strong&gt;References:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en/DOM/document.cookie"&gt;[MDC]&lt;/a&gt; &lt;code class=":js"&gt;document.cookie&lt;/code&gt;
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-3180840687650625902?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/3180840687650625902/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/06/httpcookie.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/3180840687650625902'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/3180840687650625902'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/06/httpcookie.html' title='HTTP.cookie'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-4022967731767132334</id><published>2009-06-08T22:01:00.000-07:00</published><updated>2009-07-11T00:31:19.863-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>arguments.callee</title><content type='html'>&lt;p&gt;
ECMAScript 5 strict mode has completely removed &lt;code class=":js"&gt;arguments.callee&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
Now, there have been very good arguments both &lt;a href="http://webreflection.blogspot.com/2009/05/ecmascript-5-do-not-remove.html"&gt;for&lt;/a&gt; and &lt;a href="https://mail.mozilla.org/pipermail/es-discuss/2008-September/007493.html"&gt;against&lt;/a&gt; &lt;code class=":js"&gt;arguments.callee&lt;/code&gt;, and I'm not going to go into them. However, what I will go into is how it changed my coding style, and also some possible perks.
&lt;/p&gt;&lt;p&gt;
Let's take an example of code that I showed in an earlier post:
&lt;pre class=":js"&gt;
var alert = function () {
    var that = arguments.callee;
    if (!that.stop) {
        that.stop = !confirm(Array.prototype.join.call(arguments, "\n"));
    }
};
&lt;/pre&gt;
&lt;p&gt;
Now, there isn't anything particularly wrong with this code: it works fine, and passes JSLint. However, it will surely fail in strict mode. Here is how I corrected it:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var alert = function anon() {
    if (!anon.stop) {
        anon.stop = !confirm(Array.prototype.join.call(arguments, "\n"));
    }
};
&lt;/pre&gt;
&lt;p&gt;
The above does the same thing, but without using &lt;code class=":js"&gt;arguments.callee&lt;/code&gt;. How is this possible? It uses a named anonymous function. Yes, yes, it's an oxymoron, but please bear with me here.
&lt;/p&gt;&lt;p&gt;
Most anonymous functions are.. well, anonymous. If you want to do any kind of recursion, you have to either name the function (polluting the namespace), or use &lt;code class=":js"&gt;arguments.callee&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
However, in later versions of ECMAScript, we are given the ability to make a named anonymous function. This is like a combination between a named function and an anonymous function, but with a twist: the name is only usable inside of the function.
&lt;/p&gt;&lt;p&gt;
Let's test that with some code:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var foo = function bar() {
    alert(bar);
    alert(window.bar === bar);
};
foo();
&lt;/pre&gt;
&lt;p&gt;
Note how the anonymous function &lt;code class=":js"&gt;bar()&lt;/code&gt; is accessible within itself, but does not create a global function &lt;code class=":js"&gt;bar()&lt;/code&gt;. In addition, if we assigned this function to a different variable, &lt;code class=":js"&gt;bar()&lt;/code&gt; would still be intact!
&lt;/p&gt;&lt;p&gt;
Using this technique, we no longer have any need for &lt;code class=":js"&gt;arguments.callee&lt;/code&gt;. We can use &lt;code class=":js"&gt;bar()&lt;/code&gt; within the function to refer to itself, and nothing outside has access to &lt;code class=":js"&gt;bar()&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
An interesting side effect of this is that &lt;code class=":js"&gt;alert.stop&lt;/code&gt; is &lt;code class=":js"&gt;undefined&lt;/code&gt;. In the old way you could access &lt;code class=":js"&gt;alert.stop&lt;/code&gt;. That turned out to be harmless; but in a more critical function it could have had disastrous effects.
&lt;/p&gt;&lt;p&gt;
The primary argument against this change is that it breaks stuff in IE. Except that IE has always had severe problems. It should work just fine in any non-IE browser. Also note that this only matters if you are using strict mode. If you are in normal non-strict mode, you can still use &lt;code class=":js"&gt;arguments.callee&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
Either way, I plan to use this technique much more often, as it has proven it's use.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-4022967731767132334?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/4022967731767132334/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/06/argumentscallee.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/4022967731767132334'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/4022967731767132334'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/06/argumentscallee.html' title='arguments.callee'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-1765327302011931518</id><published>2009-06-02T10:00:00.000-07:00</published><updated>2009-06-03T22:39:44.230-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='Game of Life'/><title type='text'>Game of Life in JavaScript.</title><content type='html'>&lt;p&gt;&lt;/p&gt;&lt;p&gt;
&lt;a href="http://paaru.pbworks.com/f/GameOfLife.html"&gt;[LINK]&lt;/a&gt; Conway's Game of Life
&lt;/p&gt;&lt;p&gt;
I have been fascinated by &lt;a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life"&gt;the Game of Life&lt;/a&gt; for some time now, and recently had the idea to make it in JavaScript.
&lt;/p&gt;&lt;p&gt;
Although the idea is hardly new, I am hoping that my version will end up being faster, better, and easier to use than the others. Try selecting a Template, then hit the Start button.
&lt;/p&gt;&lt;p&gt;
&lt;strong&gt;NOTE:&lt;/strong&gt; I am using &lt;a href="http://en.wikipedia.org/wiki/Canvas_(HTML_element)"&gt;canvas&lt;/a&gt; for performance reasons. As such, it won't work in Internet Explorer; but I am considering fixing that.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;UPDATE:&lt;/strong&gt; I have officially given up supporting this in IE. Microsoft has gone out of their way to &lt;em&gt;not&lt;/em&gt; support the W3C Event model, despite being given plenty of time. So, until a future version of IE decides to support the standardized events: tough luck.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-1765327302011931518?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/1765327302011931518/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/06/game-of-life-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/1765327302011931518'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/1765327302011931518'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/06/game-of-life-in-javascript.html' title='Game of Life in JavaScript.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-5265487443005794499</id><published>2009-05-30T04:21:00.000-07:00</published><updated>2009-07-11T00:28:48.233-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>Coord constructor.</title><content type='html'>&lt;p&gt;
In &lt;a href="javascript-associative-arrays.html"&gt;an earlier post&lt;/a&gt;, 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 &lt;code class=":js"&gt;Hash&lt;/code&gt; constructor to create a &lt;code class=":js"&gt;Coord&lt;/code&gt; constructor.
&lt;/p&gt;&lt;p&gt;
Here's how you create it:
&lt;/p&gt;
&lt;pre class=":js"&gt;
// Infinite constraints:
var coord = new Coord();

// Set constraints:
var coord = new Coord({
    top: 0,
    right: 50,
    bottom: 50,
    left: 0
});

// Unset constraints become infinite:
var coord = new Coord({
    right: 50,
    bottom: 50
});
&lt;/pre&gt;
&lt;p&gt;
Using the coordinate system itself is easy. You use the &lt;code class=":js"&gt;pt()&lt;/code&gt; method to get the value of a point:
&lt;/p&gt;
&lt;pre class=":js"&gt;
// Get the value of point (0,10):
coord.pt(0, 10);
&lt;/pre&gt;
&lt;p&gt;
To set or delete a point, you use the chained method &lt;code class=":js"&gt;set()&lt;/code&gt;:
&lt;pre class=":js"&gt;
// Set the value of (0,10) to the value 5:
coord.pt(0, 10).set(5);

// Delete the point (0,10):
coord.pt(0, 10).set();

// You can also delete like this:
coord.pt(0, 10).set(null);
&lt;/pre&gt;
&lt;p&gt;
By default, it has infinite constraints: you can create a new point anywhere in the coordinate system. However, if you set constraints and then try to create a new point that is outside of the constraints, it will not create the point. I use CSS syntax for familiarity.
&lt;/p&gt;&lt;p&gt;
As an example:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var coord = new Coord();
// coord.pt(-1, -1).set(1) == 1
// coord.pt(11, 11).set(1) == 1
&lt;/pre&gt;&lt;pre class=":js"&gt;
var coord = new Coord({
    right: 10,
    bottom: 10
});
// coord.pt(-1, -1).set(1) == 1
// coord.pt(11, 11).set(1) == undefined
&lt;/pre&gt;&lt;pre class=":js"&gt;
var coord = new Coord({
    top: 0,
    right: 10,
    bottom: 10,
    left: 0
});
// coord.pt(-1, -1).set(1) == undefined
// coord.pt(11, 11).set(1) == undefined
&lt;/pre&gt;
&lt;p&gt;
Using this system, you can guarantee that every point will lie within a certain range. It accepts both positive and negative numbers.
&lt;/p&gt;&lt;p&gt;
You cannot change the size of the system after it has been created. However, you can obtain the size with the &lt;code class=":js"&gt;size()&lt;/code&gt; method; which returns an object with the size properties:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var coord = new Coord({
    top: 0,
    right: 50,
    bottom: 50,
    left: 0
});
var size = coord.size();
// size.top    == 0
// size.right  == 50
// size.bottom == 50
// size.left   == 0
&lt;/pre&gt;
&lt;p&gt;
The coordinate system also provides a &lt;code class=":js"&gt;length&lt;/code&gt; property, that lists how many points are currently in the system. You can also use the &lt;code class=":js"&gt;forEach()&lt;/code&gt; method to iterate over each point:
&lt;/p&gt;
&lt;pre class=":js"&gt;
coord.forEach(function (x, y, value) {
    alert("(" + x + "," + y + ") has the value: " + value);
    this.pt(x, y).set(10);
});
&lt;/pre&gt;
&lt;p&gt;
Note how the function is given the x, y, and value of each point. In addition, &lt;code class=":js"&gt;this&lt;/code&gt; is set to the current executing coordinate system, making it easy to use the &lt;code class=":js"&gt;pt()&lt;/code&gt; method.
&lt;/p&gt;&lt;p&gt;
You can use this to implement a Cartesian coordinate system, or augment the DOM; for instance allowing collision-detection in JavaScript.
&lt;/p&gt;&lt;p&gt;
&lt;a href="http://paaru.pbworks.com/f/Coord.src.js"&gt;[LINK]&lt;/a&gt; The source code.&lt;br&gt;
&lt;a href="http://paaru.pbworks.com/f/test.Coord.html"&gt;[LINK]&lt;/a&gt; The unit tests.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-5265487443005794499?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/5265487443005794499/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/05/coord-constructor.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/5265487443005794499'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/5265487443005794499'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/05/coord-constructor.html' title='Coord constructor.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-7669225305315007239</id><published>2009-05-28T01:28:00.000-07:00</published><updated>2009-08-23T21:54:25.582-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>Timing in JavaScript.</title><content type='html'>&lt;p&gt;
&lt;strong&gt;2009/07/02 Update:&lt;/strong&gt;&lt;br&gt;
I have created a far better version, which you can find here: &lt;a href="/2009/07/timer-constructor.html"&gt;[LINK]&lt;/a&gt;
&lt;/p&gt;
&lt;pre class=":js"&gt;
"use strict";
var getTime = function (func, length) {
    var i, start, end;
    length = length || 1;
    start = new Date();
    for (i = 0; i &lt; length; i += 1) {
        func();
    }
    end = new Date();
    return end - start;
};
&lt;/pre&gt;
&lt;p&gt;
JavaScript does not have a built-in way to easily measure how long it takes to run code; however, it supplies the necessary building-blocks to create such a way.
&lt;/p&gt;&lt;p&gt;
You use &lt;code class=":js"&gt;getTime&lt;/code&gt; like this:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var time = getTime(function () {
    /* code goes here! */
}, 100000);
&lt;/pre&gt;
&lt;p&gt;
The variable &lt;code class=":js"&gt;time&lt;/code&gt; now contains how many milliseconds it took to run the function &lt;code class=":js"&gt;100000&lt;/code&gt; times.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The first parameter should be a function. The time it takes to run this function is returned.&lt;/li&gt;
&lt;li&gt;The second parameter is how many times you want to run the function. The default is &lt;code class=":js"&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Using this function, you can easily see how long it takes to run chunks of code, which is useful in benchmarking.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-7669225305315007239?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/7669225305315007239/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/05/timing-in-javascript.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/7669225305315007239'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/7669225305315007239'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/05/timing-in-javascript.html' title='Timing in JavaScript.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-2247526604576136216</id><published>2009-05-28T00:44:00.000-07:00</published><updated>2009-08-14T04:55:11.634-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>Constructors and "this".</title><content type='html'>&lt;p&gt;
&lt;b&gt;2009/08/14 Update:&lt;/b&gt;&lt;br&gt;
It has come to my attention that there is a better way of doing this:
&lt;pre class=":js"&gt;
var Point = function (x, y) {
    if (this === window) {
        return new Point(x, y);
    }
    this.x = x;
    this.y = y;
};
&lt;/pre&gt;
Although this article is still useful, please use the above, instead of using &lt;code class=":js"&gt;call&lt;/code&gt;.
&lt;/p&gt;
&lt;br&gt;
&lt;p&gt;The &lt;code class=":js"&gt;this&lt;/code&gt; keyword in JavaScript is very powerful, when used correctly. Unfortunately, JavaScript has a bit of infatuation with the global scope, so most of the time &lt;code class=":js"&gt;this&lt;/code&gt; isn't very helpful at all.
&lt;/p&gt;&lt;p&gt;
You can rebind &lt;code class=":js"&gt;this&lt;/code&gt; with the methods &lt;code class=":js"&gt;call()&lt;/code&gt; and &lt;code class=":js"&gt;apply()&lt;/code&gt;. Ordinarily, you wouldn't want to do this; however, there is one interesting case.
&lt;/p&gt;&lt;p&gt;
JavaScript constructors look like this:
&lt;/p&gt;
&lt;pre class=":js"&gt;
// Constructor:
var Point = function (x, y) {
    this.x = x;
    this.y = y;
};

// Instance:
var point = new Point(10, 5);
&lt;/pre&gt;
&lt;p&gt;
When you use the &lt;code class=":js"&gt;new&lt;/code&gt; keyword, &lt;code class=":js"&gt;this&lt;/code&gt; is bound to the newly created object, which makes it easy to set up properties on the new object.
&lt;/p&gt;&lt;p&gt;
However, if you fail to use the &lt;code class=":js"&gt;new&lt;/code&gt; keyword, &lt;code class=":js"&gt;this&lt;/code&gt; will bind the properties to the global object! This is a serious mistake. You can correct this with &lt;code class=":js"&gt;call()&lt;/code&gt; or &lt;code class=":js"&gt;apply()&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
Now you can write your constructors like this:
&lt;/p&gt;
&lt;pre class=":js"&gt;
// Constructor:
var Point = function anon(x, y) {
    if (this === window) {
        return anon.call({});
    }
    this.x = x;
    this.y = y;
    return this;
};

// Instance:
var point = new Point(10, 5);

// Instance:
var point = Point(10, 5);
&lt;/pre&gt;
&lt;p&gt;
The &lt;code class=":js"&gt;if&lt;/code&gt; and &lt;code class=":js"&gt;return this;&lt;/code&gt; statements are mandatory. This is used to ensure that if you accidentally leave off &lt;code class=":js"&gt;new&lt;/code&gt;, it will not bind the properties to the global object.
&lt;/p&gt;
&lt;p&gt;
Of course, you could have written it like this:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var Point = function (x, y) {
    return {
        x: x,
        y: y
    };
};
&lt;/pre&gt;
&lt;p&gt;
Which has the same effect. However, now you give up the ability to use &lt;code class=":js"&gt;call&lt;/code&gt; and &lt;code class=":js"&gt;apply&lt;/code&gt; to change the &lt;code class=":js"&gt;this&lt;/code&gt; binding. In addition, you can't use prototypical inheritance anymore! If you don't care about that, then feel free to write your constructors without &lt;code class=":js"&gt;this&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
However, if you do care, then you can use the above construct to allow the use of &lt;code class=":js"&gt;this&lt;/code&gt;, without worrying about binding to the global object.
&lt;/p&gt;&lt;p&gt;
This will no longer apply in ECMAScript 5 strict mode, which throws an error when you try to use a constructor without &lt;code class=":js"&gt;new&lt;/code&gt;. 
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-2247526604576136216?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/2247526604576136216/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/05/constructors-and-this.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/2247526604576136216'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/2247526604576136216'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/05/constructors-and-this.html' title='Constructors and &quot;this&quot;.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-8051856551901953086</id><published>2009-05-26T09:40:00.000-07:00</published><updated>2009-07-11T00:19:30.472-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>JavaScript associative arrays.</title><content type='html'>&lt;p&gt;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 "associative array."
&lt;/p&gt;&lt;p&gt;
The problem with objects being hashes is that it tends to "break" the for-in loop. If you add a property to the prototype chain of an object, it will be enumerated in for-in loops. You can, of course, use an &lt;code class=":js"&gt;if&lt;/code&gt; statement to filter out the prototype chain, but this is cumbersome.
&lt;/p&gt;&lt;p&gt;
Enter the &lt;code class=":js"&gt;Hash&lt;/code&gt; constructor. I personally have never had need for a genuine hash, but I had fun making this, and hope it's useful for somebody. My goal was to implement a simple and lightweight system that would make use of closures and interfaces.
&lt;/p&gt;&lt;p&gt;
You initialize it with &lt;code class=":js"&gt;new Hash()&lt;/code&gt;, and you can pass along an object as a shorthand.
&lt;/p&gt;
&lt;pre class=":js"&gt;
var hash = new Hash();
hash.key("test", "Hi!");
&lt;/pre&gt;
&lt;p&gt;
Is the same as:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var hash = new Hash({
    test: "Hi!"
});
&lt;/pre&gt;
&lt;p&gt;
Each instance of the &lt;code class=":js"&gt;Hash&lt;/code&gt; constructor has it's own private variable &lt;code class=":js"&gt;values&lt;/code&gt;, which you cannot touch. Instead, you use the &lt;code class=":js"&gt;key()&lt;/code&gt; method to read, write, and delete values. It works like this:
&lt;/p&gt;
&lt;pre class=":js"&gt;
hash.key("test");
// Get the value of the key "test"

hash.key("test", "Hi!");
// Set the value of the key "test"

hash.key("test", null);
// Delete the value of the key "test"
&lt;/pre&gt;
&lt;p&gt;
If you specify only one argument, it will return the value in the key. If you specify two arguments, it will set the value of the key. If you set a key to the value &lt;code class=":js"&gt;null&lt;/code&gt;, it will delete the key, so it no longer exists.
&lt;/p&gt;&lt;p&gt;
In addition, every &lt;code class=":js"&gt;Hash&lt;/code&gt; instance has a &lt;code class=":js"&gt;length&lt;/code&gt; property (like an array) that will tell you how many keys are in the hash. You are free to set this property to whatever you want (or even delete it), but it will have no effect. The actual length is a private variable; so next time you set or delete a key, the &lt;code class=":js"&gt;length&lt;/code&gt; property will be overwritten with the correct length.
&lt;/p&gt;&lt;p&gt;
This is all very nice, of course, but I went a step further: a &lt;code class=":js"&gt;forEach()&lt;/code&gt; method. This method will loop over the keys in the hash, and run the function you specify on each one. The two arguments it passes are the name and value of the key. In addition, &lt;code class=":js"&gt;this&lt;/code&gt; is bound to the current hash, making it easy to manipulate the keys.
&lt;/p&gt;&lt;p&gt;
Here's an example of a simple way to delete all the keys in a hash:
&lt;/p&gt;
&lt;pre class=":js"&gt;
hash.forEach(function (name) {
    this.key(name, null);
});
&lt;/pre&gt;
&lt;p&gt;
And here's a way to obtain the name and value of all the keys in a hash:
&lt;/p&gt;
&lt;pre class=":js"&gt;
hash.forEach(function (name, value) {
    alert("Key " + name + " has the value: " + value);
});
&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://paaru.pbworks.com/f/Hash.src.js"&gt;[LINK]&lt;/a&gt; The source code.&lt;br&gt;
&lt;a href="http://paaru.pbworks.com/f/test.Hash.html"&gt;[LINK]&lt;/a&gt; The unit tests.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-8051856551901953086?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/8051856551901953086/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/05/javascript-associative-arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/8051856551901953086'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/8051856551901953086'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/05/javascript-associative-arrays.html' title='JavaScript associative arrays.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-8387027915054929951</id><published>2009-05-09T14:25:00.000-07:00</published><updated>2010-03-18T04:47:44.684-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>The &amp;&amp; and || operators.</title><content type='html'>&lt;p&gt;
In JavaScript, the &lt;code class=":js"&gt;&amp;&amp;&lt;/code&gt; and &lt;code class=":js"&gt;||&lt;/code&gt; operators are used as short-hands for conditionals. For instance:
&lt;/p&gt;
&lt;pre class=":js"&gt;
if (foo) {
    if (bar) {
    }
}
&lt;/pre&gt;
Can be written much more succinctly like this:
&lt;pre class=":js"&gt;
if (foo &amp;&amp; bar) {
}
&lt;/pre&gt;
And:
&lt;pre class=":js"&gt;
if (foo) {
} else if (bar) {
}
&lt;/pre&gt;
Can be written like this:
&lt;pre class=":js"&gt;
if (foo || bar) {
}
&lt;/pre&gt;
That's great and all, but these two simple operators hold even more power to them. Let's look at exactly how they function:
&lt;ul&gt;
&lt;li&gt;
&lt;code class=":js"&gt;&amp;&amp;&lt;/code&gt; returns the first operand if it can be converted to &lt;code class=":js"&gt;false&lt;/code&gt;; otherwise, returns the second operand.
&lt;/li&gt;
&lt;li&gt;
&lt;code class=":js"&gt;||&lt;/code&gt; returns the first operand if it can be converted to &lt;code class=":js"&gt;true&lt;/code&gt;; otherwise, returns the second operand.
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The key thing to note here is the second clause. &lt;code class=":js"&gt;&amp;&amp;&lt;/code&gt; and &lt;code class=":js"&gt;||&lt;/code&gt; do not always return boolean values! That works out okay, because JavaScript has the concept of truthy and falsy values, which is a fancy way to say that anything can be easily converted into &lt;code class=":js"&gt;true&lt;/code&gt; or &lt;code class=":js"&gt;false&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
"What are you talking about? Why should I care about this?", you say. Let's look at some examples of code, and show how the &lt;code class=":js"&gt;&amp;&amp;&lt;/code&gt; and &lt;code class=":js"&gt;||&lt;/code&gt; operators can help us out.
&lt;/p&gt;
&lt;pre class=":js"&gt;
if (foo) {
    foo.bar();
}
&lt;/pre&gt;
Okay, looks simple enough. We don't know whether &lt;code class=":js"&gt;foo&lt;/code&gt; exists at runtime or not, so we have to wrap the statement &lt;code class=":js"&gt;foo.bar()&lt;/code&gt; in an &lt;code class=":js"&gt;if&lt;/code&gt;. However, the following code is identical:
&lt;pre class=":js"&gt;
foo &amp;&amp; foo.bar();
&lt;/pre&gt;
&lt;p&gt;
"Huh? What's going on here?" As stated above, the &lt;code class=":js"&gt;&amp;&amp;&lt;/code&gt; operator returns the second operand if the first operand is truthy. In addition, if the first operand is falsy, the second operand isn't even evaluated!
&lt;/p&gt;&lt;p&gt;
In other words: &lt;code class=":js"&gt;foo.bar()&lt;/code&gt; is only called if &lt;code class=":js"&gt;foo&lt;/code&gt; is truthy.
&lt;/p&gt;&lt;p&gt;
What exactly is truthy and falsy? A value in JavaScript is falsy if it is &lt;code class=":js"&gt;undefined&lt;/code&gt;, &lt;code class=":js"&gt;null&lt;/code&gt;, &lt;code class=":js"&gt;false&lt;/code&gt;, the empty string &lt;code class=":js"&gt;""&lt;/code&gt;, &lt;code class=":js"&gt;NaN&lt;/code&gt;, or the number &lt;code class=":js"&gt;0&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
&lt;i&gt;Everything&lt;/i&gt; else is truthy, including an empty object &lt;code class=":js"&gt;{}&lt;/code&gt; and empty array &lt;code class=":js"&gt;[]&lt;/code&gt;.
&lt;/p&gt;&lt;p&gt;
"Fine, so I can save a couple lines by using the &lt;code class=":js"&gt;&amp;&amp;&lt;/code&gt; operator, who cares?"
&lt;/p&gt;&lt;p&gt;
Let's make this example a little more complicated by using a more deeply-nested object:
&lt;/p&gt;
&lt;pre class=":js"&gt;
if (foo) {
    if (foo.bar) {
        if (foo.bar.qux) {
            foo.bar.qux.corge();
        }
    }
}
&lt;/pre&gt;
Oh my! Look at all the nested &lt;code class=":js"&gt;if&lt;/code&gt;s! Now, we could have written it like this:
&lt;pre class=":js"&gt;
if (foo &amp;&amp; foo.bar &amp;&amp; foo.bar.qux) {
    foo.bar.qux.corge();
}
&lt;/pre&gt;
Or even more succinctly:
&lt;pre class=":js"&gt;
foo &amp;&amp; foo.bar &amp;&amp; foo.bar.qux &amp;&amp; foo.bar.qux.corge();
&lt;/pre&gt;
&lt;p&gt;
Whichever you use will depend on your personal preferences. I'm not here to convince you to use the logical operators in this way, merely to point out that some people do use them this way. So next time you see something like the code above, you'll know what's going on, rather than being confused.
&lt;/p&gt;&lt;p&gt;
Now, the &lt;code class=":js"&gt;||&lt;/code&gt; operator can be used to select the "default" value of a variable. What do I mean by this? Well, imagine you have a function that accepts arguments, but some arguments are optional. Here's an example:
&lt;/p&gt;
&lt;pre class=":js"&gt;
function foo(a, b, c) {
    if (!c) {
        c = 10;
    }
}
&lt;/pre&gt;
Now, there's nothing wrong with this, but you can save some characters by doing it like this:
&lt;pre class=":js"&gt;
function foo(a, b, c) {
    c = c || 10;
}
&lt;/pre&gt;
&lt;p&gt;
Using the above code, the variable &lt;code class=":js"&gt;c&lt;/code&gt; will supply the default value &lt;code class=":js"&gt;10&lt;/code&gt; if &lt;code class=":js"&gt;c&lt;/code&gt; is not specified when calling the function.
&lt;/p&gt;&lt;p&gt;
As stated above, my goal is not to convince you to use these techniques, merely to point them out so you'll understand what's going on when you read other people's code.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-8387027915054929951?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/8387027915054929951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/05/and-operators.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/8387027915054929951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/8387027915054929951'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/05/and-operators.html' title='The &amp;&amp; and || operators.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-4990850884349425022</id><published>2009-04-28T03:45:00.001-07:00</published><updated>2009-07-22T21:20:23.023-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>Essential JavaScript functions.</title><content type='html'>&lt;b&gt;2009/07/22 Update:&lt;/b&gt;&lt;br&gt;
Added &lt;code class=":js"&gt;Object.keys&lt;/code&gt;. Removed &lt;code class=":js"&gt;String.prototype.*&lt;/code&gt;, &lt;code class=":js"&gt;alert&lt;/code&gt;, and &lt;code class=":js"&gt;waitUntil&lt;/code&gt;.
&lt;pre class=":js"&gt;
"use strict";
if (typeof Object.create !== "function") {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}
if (typeof Object.keys !== "function") {
    Object.keys = function (obj) {
        var name, output = [];
        for (name in obj) {
            if (Object.prototype.hasOwnProperty.call(obj, name)) {
                output.push(name);
            }
        }
        return output;
    };
}
&lt;/pre&gt;
&lt;p&gt;
The above are functions I have found indispensable or extremely useful in my programming. They are listed here for convenience.
&lt;/p&gt;
&lt;a href="http://javascript.crockford.com/prototypal.html"&gt;Object.create&lt;/a&gt;
&lt;br&gt;&lt;br&gt;
&lt;del&gt;&lt;a href="stringprototype.html"&gt;String.prototype&lt;/a&gt;&lt;/del&gt;
&lt;br&gt;
&lt;del&gt;&lt;a href="windowalert.html"&gt;window.alert&lt;/a&gt;&lt;/del&gt;
&lt;br&gt;
&lt;del&gt;&lt;a href="windowwaituntil.html"&gt;window.waitUntil&lt;/a&gt;&lt;/del&gt;
&lt;p&gt;&lt;/p&gt;
&lt;del&gt;&lt;p&gt;
&lt;a href="http://paaru.pbworks.com/f/Essentials.src.js"&gt;[LINK]&lt;/a&gt; The source code.&lt;br&gt;
&lt;a href="http://paaru.pbworks.com/f/test.Essentials.html"&gt;[LINK]&lt;/a&gt; The unit tests.
&lt;/p&gt;&lt;/del&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-4990850884349425022?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/4990850884349425022/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/04/essential-javascript-functions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/4990850884349425022'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/4990850884349425022'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/04/essential-javascript-functions.html' title='Essential JavaScript functions.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-4210865816740299320</id><published>2009-04-28T02:41:00.000-07:00</published><updated>2009-08-23T22:09:10.597-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>window.waitUntil</title><content type='html'>&lt;b&gt;2009/08/23 Update:&lt;/b&gt;&lt;br&gt;
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 &lt;code class=":js"&gt;(function () {}());&lt;/code&gt; construct.
&lt;p&gt;
Here is a new function I have recently created:
&lt;pre class=":js"&gt;
"use strict";
var waitUntil = function anon(func, ms) {
    ms = ms || 100;
    if (func() !== true) {
        return setTimeout(function () {
            anon(func, ms);
        }, ms);
    }
};
&lt;/pre&gt;
&lt;/p&gt;&lt;p&gt;
I don't know about you, but in my code I have to frequently test a condition... but do so asynchronously. You can do this with &lt;code class=":js"&gt;setTimeout&lt;/code&gt;, but it requires a lot of syntax and work to get it right. The above function attempts to ease this a little.
&lt;/p&gt;&lt;p&gt;
You call it like so:
&lt;/p&gt;
&lt;pre class=":js"&gt;
waitUntil(function () {
    if (test === 5) {
        alert("Done!");
        return true;
    }
});
&lt;/pre&gt;
This is the same as if I had done:
&lt;pre class=":js"&gt;
(function anon() {
    if (test === 5) {
        alert("Done");
    } else {
        setTimeout(anon, 100);
    }
}());
&lt;/pre&gt;
&lt;p&gt;
Not a huge difference, but still useful when you need to perform such checks on a regular basis. &lt;code class=":js"&gt;waitUntil&lt;/code&gt; accepts two arguments:
&lt;ol&gt;
&lt;li&gt;This should be a function. &lt;code class=":js"&gt;waitUntil&lt;/code&gt; will continue executing this function until it returns &lt;code class=":js"&gt;true&lt;/code&gt;, which tells &lt;code class=":js"&gt;waitUntil&lt;/code&gt; to stop.&lt;/li&gt;
&lt;p&gt;&lt;/p&gt;
&lt;li&gt;This should be a number. This is how often &lt;code class=":js"&gt;waitUntil&lt;/code&gt; will run the function, in milliseconds. The default is &lt;code class=":js"&gt;100&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/p&gt;
&lt;p&gt;
In addition, &lt;code class=":js"&gt;waitUntil&lt;/code&gt; returns a &lt;code class=":js"&gt;setTimeout&lt;/code&gt;, so you can use &lt;code class=":js"&gt;clearTimeout&lt;/code&gt; to halt the check at any time:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var timer = waitUntil(function () {
    if (test === 5) {
        alert("Done!");
        return true;
    }
}, 300);
clearTimeout(timer);
&lt;/pre&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;
&lt;a href="http://paaru.pbworks.com/f/test.Essentials.html"&gt;[LINK]&lt;/a&gt; The unit tests.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-4210865816740299320?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/4210865816740299320/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/04/windowwaituntil.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/4210865816740299320'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/4210865816740299320'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/04/windowwaituntil.html' title='window.waitUntil'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-5879912768089948735</id><published>2009-04-28T02:33:00.001-07:00</published><updated>2009-07-11T00:00:49.257-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>window.alert</title><content type='html'>One of the problems with the &lt;code class=":js"&gt;alert()&lt;/code&gt; function is the inability to cancel it when placed within loops. You may find this function useful:
&lt;pre class=":js"&gt;
"use strict";
var alert = function anon() {
    if (!anon.stop) {
        anon.stop = !confirm(Array.prototype.join.call(arguments, "\n"));
    }
};
&lt;/pre&gt;
These following lines of code are identical:
&lt;pre class=":js"&gt;
// Old:
alert("Hi!");
// New:
alert("Hi!");
&lt;/pre&gt;
&lt;pre class=":js"&gt;
// Old:
alert(1 + "\n" + 2 + "\n" + 3);
// New:
alert(1, 2, 3);
&lt;/pre&gt;
&lt;p&gt;
However, an even more powerful feature of this new &lt;code class=":js"&gt;alert()&lt;/code&gt; function is that if it is called repeatedly, you can click the "Cancel" button to prevent it from displaying any more alerts!
&lt;/p&gt;&lt;p&gt;
Credit for the idea goes to &lt;a href="http://www.mennovanslooten.nl/blog/post/12"&gt;this blog&lt;/a&gt;. The actual code is all by me, though.
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;
&lt;a href="http://paaru.pbworks.com/f/test.Essentials.html"&gt;[LINK]&lt;/a&gt; The unit tests.
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-5879912768089948735?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/5879912768089948735/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/04/windowalert.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/5879912768089948735'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/5879912768089948735'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/04/windowalert.html' title='window.alert'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-6808938498483439539</id><published>2009-04-12T17:27:00.000-07:00</published><updated>2009-07-10T23:43:25.469-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Mathematics'/><title type='text'>Reflecting a vector.</title><content type='html'>Let us assume:
&lt;pre class=":formula"&gt;
angle = the angle of the incoming vector.
 wall = the angle of the "wall".
&lt;/pre&gt;
&lt;br /&gt;
The formula for the reflection of an incoming vector in degrees is:
&lt;pre class=":formula"&gt;
angle - (2 * (angle + (90 - wall)))
&lt;/pre&gt;
There is also a version in radians, which is very similar:
&lt;pre class=":formula"&gt;
angle - (2 * (angle + ((PI / 2) - wall)))
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-6808938498483439539?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/6808938498483439539/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/04/reflecting-vector.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/6808938498483439539'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/6808938498483439539'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/04/reflecting-vector.html' title='Reflecting a vector.'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6133409975588434312.post-3329400177476350330</id><published>2009-04-06T02:39:00.000-07:00</published><updated>2009-07-10T23:58:17.175-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><title type='text'>String.prototype</title><content type='html'>I have created three (very) simple JavaScript methods for working with strings. Any competent JavaScript programmer could whip these up in 5 minutes; nonetheless, they are useful so I shall share them with you:
&lt;pre class=":js"&gt;
"use strict";
if (typeof String.prototype.sort !== "function") {
    String.prototype.sort = function (funct) {
        return this.split("").sort(funct).join("");
    };
}
if (typeof String.prototype.splice !== "function") {
    String.prototype.splice = function (index, length, add) {
        var text = this.split("");
        text.splice(index, length, add);
        return text.join("");
    };
}
if (typeof String.prototype.reverse !== "function") {
    String.prototype.reverse = function () {
        return this.split("").reverse().join("");
    };
}
&lt;/pre&gt;
&lt;p&gt;
Let's examine them. All three make use of the &lt;code class=":js"&gt;string.split()&lt;/code&gt; and &lt;code class=":js"&gt;array.join()&lt;/code&gt; methods. These convert a string to an array and an array to a string, respectively.
&lt;/p&gt;
&lt;p&gt;
By passing an empty string &lt;code class=":js"&gt;""&lt;/code&gt; as the single argument, it creates an array where each index holds a single character of the string. Thus:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var text = "Hi!";

text = text.split("");
// text[0] == "H"
// text[1] == "i"
// text[2] == "!"

text = text.join("");
// text == "Hi!"
&lt;/pre&gt;
&lt;p&gt;
Useless? Not so! This allows us to treat a string as an array of characters, and thus pretend that strings are mutable. As a simple example:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var text = "Testing";
text = text.split("");

text[0] = "a";
text[1] = "b";
text[2] = "c";

text = text.join("");
// text == "abcting"
&lt;/pre&gt;
&lt;p&gt;
You can also use this to delete characters at specific indices by setting the index to &lt;code class=":js"&gt;null&lt;/code&gt; or &lt;code class=":js"&gt;""&lt;/code&gt;. In this way, you can manipulate strings.
&lt;/p&gt;
&lt;p&gt;
This is very useful, but my methods go a step further by using the built-in &lt;code class=":js"&gt;Array&lt;/code&gt; methods. After adding the above three methods to your program, you can do stuff like this:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var text = "Testing";
text = text.reverse();
// text == "gnitseT"
&lt;/pre&gt;
&lt;p&gt;
Or you can use the powerful &lt;code class=":js"&gt;array.splice()&lt;/code&gt; method on strings:
&lt;/p&gt;
&lt;pre class=":js"&gt;
var text = "Testing";
text = text.splice(3, 2).splice(0, 1, "A");
// text == "Aesng"
&lt;/pre&gt;
&lt;p&gt;
The first splice removes the characters at index &lt;code class=":js"&gt;3&lt;/code&gt; and &lt;code class=":js"&gt;4&lt;/code&gt; (remember, the second argument is the length, not the index!). The second splice removes the first index and inserts the string &lt;code class=":js"&gt;"A"&lt;/code&gt;, producing the final result. Because all three methods return strings, you can chain them together, as shown above.
&lt;/p&gt;
&lt;p&gt;
There are two differences between &lt;code class=":js"&gt;string.splice()&lt;/code&gt; and &lt;code class=":js"&gt;array.splice()&lt;/code&gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;code class=":js"&gt;string.splice()&lt;/code&gt; returns a string, which is the source string with the splice method applied. On the other hand, &lt;code class=":js"&gt;array.splice()&lt;/code&gt; returns an array of the removed values.
&lt;/li&gt;
&lt;p&gt;&lt;/p&gt;
&lt;li&gt;
&lt;p&gt;
&lt;code class=":js"&gt;string.splice()&lt;/code&gt; accepts up to 3 arguments. It ignores any arguments beyond the 3rd. &lt;code class=":js"&gt;array.splice()&lt;/code&gt; accepts an unlimited number of arguments.
&lt;/p&gt;
&lt;p&gt;
This is not a bug. These two lines are the same:
&lt;br&gt;
&lt;code class=":js"&gt;string.splice(0, 0, "a", "b", "c");&lt;/code&gt;
&lt;br&gt;
&lt;code class=":js"&gt;string.splice(0, 0, "a" + "b" + "c");&lt;/code&gt;&lt;br&gt;
Because extra arguments are unnecessary, I snipped them out for performance reasons.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/p&gt;
&lt;p&gt;
Beyond those two differences, &lt;code class=":js"&gt;string.splice()&lt;/code&gt; performs identically to &lt;code class=":js"&gt;array.splice()&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
The third method is &lt;code class=":js"&gt;string.sort()&lt;/code&gt;. This performs identically to &lt;code class=":js"&gt;array.sort()&lt;/code&gt;, so please consult your favorite JavaScript manual for information on how to use it.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Note:&lt;/strong&gt; I did not include the other &lt;code class=":js"&gt;Array&lt;/code&gt; methods because they are either useless (like &lt;code class=":js"&gt;array.concat()&lt;/code&gt;), can be simulated with &lt;code class=":js"&gt;array.splice()&lt;/code&gt; (&lt;code class=":js"&gt;array.pop()&lt;/code&gt;, &lt;code class=":js"&gt;array.push()&lt;/code&gt;, etc.), or already have a &lt;code class=":js"&gt;String&lt;/code&gt; equivalent (&lt;code class=":js"&gt;array.slice()&lt;/code&gt;).
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Note:&lt;/strong&gt; Beyond the two differences listed for &lt;code class=":js"&gt;string.splice()&lt;/code&gt;, all three methods perform identically to their &lt;code class=":js"&gt;Array&lt;/code&gt; counterparts.
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;
&lt;a href="http://paaru.pbworks.com/f/test.Essentials.html"&gt;[LINK]&lt;/a&gt; The unit tests.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;References:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/Sort"&gt;[MDC]&lt;/a&gt; &lt;a href="http://w3schools.com/jsref/jsref_sort.asp"&gt;[w3]&lt;/a&gt; &lt;code class=":js"&gt;array.sort()&lt;/code&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/splice"&gt;[MDC]&lt;/a&gt; &lt;a href="http://www.w3schools.com/jsref/jsref_splice.asp"&gt;[w3]&lt;/a&gt; &lt;code class=":js"&gt;array.splice()&lt;/code&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/reverse"&gt;[MDC]&lt;/a&gt; &lt;a href="http://w3schools.com/jsref/jsref_reverse.asp"&gt;[w3]&lt;/a&gt; &lt;code class=":js"&gt;array.reverse()&lt;/code&gt;
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6133409975588434312-3329400177476350330?l=kaescripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://kaescripts.blogspot.com/feeds/3329400177476350330/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://kaescripts.blogspot.com/2009/04/stringprototype.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/3329400177476350330'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6133409975588434312/posts/default/3329400177476350330'/><link rel='alternate' type='text/html' href='http://kaescripts.blogspot.com/2009/04/stringprototype.html' title='String.prototype'/><author><name>Pauan</name><uri>http://www.blogger.com/profile/09141136258826180195</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
