Skip to content

Instantly share code, notes, and snippets.

@creationix
Created July 7, 2010 01:36
Show Gist options
  • Save creationix/466174 to your computer and use it in GitHub Desktop.
Save creationix/466174 to your computer and use it in GitHub Desktop.
// 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 = shape.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);
@MartynDavis
Copy link

line # 13 should read "var baseToString = obj.toString" (as pointed out elsewhere)

@futur - doing so would create an infinite loop and blow the stack since you are redefining toString() to a allow the value of 'r' to be displayed.

@MaybeALittleLate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment