Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fiveminuteargument/2367243 to your computer and use it in GitHub Desktop.
Save fiveminuteargument/2367243 to your computer and use it in GitHub Desktop.
A simple 'OO' closure example
// Requires node v0.1.100 or a browser with console
function newShape(x, y) {
return {
toString: function () {
return 'Shape at ' + x + ', ' + y;
}
};
}
function newCircle(x, y, r) {
var obj = newShape(x, y);
var baseToString = obj.toString;
obj.toString = function () {
return 'Circular '+ baseToString() + ' with radius ' + r;
};
return obj;
}
var shape = newShape(10, 20);
console.log(shape + "");
var circle = newCircle(15, 40, 10);
console.log(circle + "");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment