Skip to content

Instantly share code, notes, and snippets.

@mneedham
Created February 27, 2010 18:53
Show Gist options
  • Save mneedham/316879 to your computer and use it in GitHub Desktop.
Save mneedham/316879 to your computer and use it in GitHub Desktop.
Playing with some code to understand overriding.
This works:
function Foo() {}
Foo.prototype = {
bar : function() {
console.log("original bar");
}
}
var originalBar = Foo.prototype.bar;
Foo.prototype = {
bar : function() {
originalBar.apply(this, arguments);
console.log("new bar");
}
}
new Foo().bar();
> original bar
> new bar
But I don't want 'originalBar' to be exposed so I try to use the proxy pattern:
(function() {
var originalBar = Foo.prototype.bar;
Foo.prototype = {
bar : function() {
originalBar.apply(this, arguments);
console.log("new bar");
}
}
})();
new Foo().bar();
> TypeError: ({bar:(function () {console.log("original bar");})}) is not a function
Any ideas?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment