Of Linux, Programming, and Singaporean Ramblings
Wed, Sep 28

A little lesson on binding

I’ve been writing JavaScript for the past 10 years for my work, and there are still things in the language that trips me up. A very good example is on binding.

Given the setup below, I would have been a little hesitant as to what the right answer is.

function Alpha() { = 'A';

Alpha.prototype.say = function () {

function Beta() { = 'B';

Beta.prototype.say = function () {

var name = 'G';

function say() {

function echo(action) {

a = new Alpha();
b = new Beta();

a.say(); // returns A
b.say(); // returns B
say(); // returns G

Nothing fancy up to this point. But the next 2 lines will make me think a bit.

// Output: 'G'
// Output: 'A'

This next part is the kicker.

function Delta() { = 'D';
    this.say = this.say.bind(this);

Delta.prototype.say = function () {

d = new Delta();


What do you think the output is?


Both lines output “D”.

This example shows that there is a case for early binding even on prototype methods.

Using your Camera on a Webpage

This page provides a working demonstration of the HTML5 getUserMedia API. If your browser supports it, when you click "Start", the page will prompt you for permission to use the camera. If you are using a phone, it might even ask you if you want to share the back-facing for the front-facing camera.

Touch Swipe Plugin

This is a plugin that I had written for SitePoint relating to the use of touch interface for swiping elements horizontally.

There are two examples (Demo One & Demo Two) for each of two-part articles at Part One and Part Two.

Cactus App

Cactus App a simple Web app designed to help simplify the calculation of pro-rated salary. It helps the user by removing the need to perform workday calculation for a month.

Right now, it only handles the case where the workdays are from Mondays to Fridays.