Fun with inheritFrom()
by deanpeters
HTML
<h1>FUn with inheritFrom()</h1>
<p>Use Your Google Chrome / FireFox Firebug Console to see anything</p>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-442503-9']);
_gaq.push(['_setDomainName', 'deanpeters.com']);
_gaq.push(['_setCampNameKey', 'jsfiddle']);
_gaq.push(['_setCampMediumKey', navigator.userAgent ]);
_gaq.push(['_setCampSourceKey', 'internet']);
_gaq.push(['_setCampTermKey', 'javascript']);
_gaq.push(['_setCampContentKey', document.title ]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
JavaScript
var pet = function(name, sound) {
this.petName = name;
this.petSound = sound;
this.speak = function(msg) {
msg = (!msg) ? this.petSound : msg;
console.log(this.petName + " says " + msg);
}
}
var dog = function(breed, name, sound) {
this.inheritFrom = pet;
this.inheritFrom(name + " the " + breed, sound);
}
dog.prototype.name = function() {
return this.petName;
}
var turtle = new pet("mamma", "ffffttp");
turtle.speak();
var snoopy = new dog("beagle", "snoopy", "woof!");
snoopy.speak();
console.log(snoopy.name());