Created
March 3, 2012 05:06
-
-
Save cormacrelf/1964487 to your computer and use it in GitHub Desktop.
Prototypical inheritance
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
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