Skip to content

Instantly share code, notes, and snippets.

@cormacrelf
Created March 3, 2012 05:06
Show Gist options
  • Save cormacrelf/1964487 to your computer and use it in GitHub Desktop.
Save cormacrelf/1964487 to your computer and use it in GitHub Desktop.
Prototypical inheritance
function Foo() {
this.meaningOfLife = 42;
this.hasOwnProperty = function() {
return false;
};
}
Foo.prototype = {
wat: function() {
return Array(16).join("wat" - 1) + " Batman!";
}
};
Foo.prototype.foobar = function() {
var r = function(f) {return f;};
return r((function() {return Foo.prototype['wat']();})());
};
Bar.prototype = new Foo();
Bar.prototype.confusedYet = Foo.prototype.foobar;
Bar.prototype.constructor = Bar;
Object.prototype.meaningOfLife = Bar.meaningOfLife - 1;
var dog = new Bar();
if (({
method_args: ["Foo", Bar.prototype.meaningOfLife]
}).hasOwnProperty.call(dog, 'foo')) {
console.log(dog.confusedYet());
}
function Bar() {
this.foo = 'Oh hai';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment