-
-
Save pvdz/410224 to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Prototypal inheritence</title> | |
</head> | |
<body> | |
<form> | |
<div><button onclick="animal.speak();return false;">animal.speak()</button></div> | |
<div><button onclick="dog.speak();return false;">dog.speak()</button></div> | |
<div><button onclick="dog.bark();return false;">dog.bark()</button></div> | |
<div><button onclick="chihuahua.speak();return false;">chihuahua.speak()</button></div> | |
<div><button onclick="chihuahua.bark();return false;">chihuahua.bark()</button></div> | |
<div><button onclick="Dog.prototype.bark.call(chihuahua);return false;">Dog.prototype.bark.call(chihuahua)</button></div> | |
</form> | |
<p id="log"></p> | |
<script type="text/javascript"> | |
var document = this.document; | |
function Animal() { | |
this.creatureType = 'animal'; | |
this.sound = 'squawk'; | |
} | |
Animal.prototype.speak = function () { | |
document.getElementById('log').innerHTML += 'my sound is a ' + this.sound + '<br>'; | |
}; | |
function Dog() { | |
Animal.apply(this,arguments); | |
this.sound = 'woof'; | |
} | |
Dog.prototype = new Animal(); | |
Dog.prototype.bark = function () { | |
document.getElementById('log').innerHTML += 'i go ' + this.sound + '<br>'; | |
}; | |
function Chihuahua() { | |
Animal.apply(this,arguments); | |
this.sound = 'yip'; | |
} | |
Chihuahua.prototype = new Dog(); | |
Chihuahua.prototype.bark = function () { | |
document.getElementById('log').innerHTML += 'yo barko ' + this.sound + '<br>'; | |
}; | |
var animal, dog, chihuahua; | |
animal = new Animal(); | |
dog = new Dog(); | |
chihuahua = new Chihuahua(); | |
alert(dog.creatureType); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment