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
  • Select an option

  • Save wilmoore/765954 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
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