Inheritance/Single: Difference between revisions

m
Line 998:
=={{header|JavaScript}}==
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance.
<syntaxhighlight lang="javascript">function Animal() {
function Animal() {
// ...
}
}</syntaxhighlight>
 
<syntaxhighlight lang="javascript">function Dog() {
// ...
}
Dog.prototype = new Animal();</syntaxhighlight>
 
<syntaxhighlight lang="javascript">function Cat() {
// ...
}
Cat.prototype = new Animal();</syntaxhighlight>
 
<syntaxhighlight lang="javascript">function Collie() {
// ...
}
Collie.prototype = new Dog();</syntaxhighlight>
 
<syntaxhighlight lang="javascript">function Lab() {
// ...
}
Lab.prototype = new Dog();</syntaxhighlight>
}</syntaxhighlight>
 
<syntaxhighlight lang="javascript">Animal.prototype.speak = function() {print("an animal makes a sound")};
7,794

edits