Created
July 7, 2010 01:36
-
-
Save creationix/466174 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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