Skip to content

Instantly share code, notes, and snippets.

@creationix
Created July 7, 2010 01:36
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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);
@futur
Copy link

futur commented Jun 5, 2013

Hi,

Line number : 13, instead of that you can straight away use "return 'Circular '+ shape.toString() + ' with radius ' + r;" at line number 15 isnt it ? did you do it that way for any particular purpose ?

@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