Duck-typing and inheritance

// http://stackoverflow.com/a/12767496/475726
function OceanAnimal() {
    this.swim = function (n) {
        return "I am " + this.name + ", the " + this.type +
               ", and I just swam " + n + " meters.";
    };
}
 
function LandAnimal() {
    this.walk = function (n) {
        return "I am " + this.name + ", the " + this.type +
               ", and I just walked " + n + " meters.";
    };
}
 
function Turtle(name) {
    this.name = name;
    LandAnimal.call(this);
    OceanAnimal.call(this);
}
 
function Lion(name) {
    this.name = name;
    LandAnimal.call(this);
}
 
var yoyo = new Turtle("Yoyo");
var simba = new Lion("Simba");
 
alert(yoyo.walk(10));
alert(yoyo.swim(30));   // turtles are faster in the water
alert(simba.walk(20));
 
/disk_1/www/cz/ladariha/wiki/data/pages/programovani/javascript/duck-typing.txt · Poslední úprava: 2013/07/08 12:23 autor: ladariha
 
Kromě míst, kde je explicitně uvedeno jinak, je obsah této wiki licencován pod následující licencí:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki