Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Forked from creationix/closure_inheritance.js
Created January 5, 2011 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilmoore/765954 to your computer and use it in GitHub Desktop.
Save wilmoore/765954 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);
@wilmoore
Copy link
Author

wilmoore commented Jan 5, 2011

closures and inheritance

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