inherit Prototype helper method - Node.js Object

Node.js examples for Object:Prototype

Description

inherit Prototype helper method

Demo Code


var animal, /*from w  w w  .  j av a  2  s  .c  o  m*/
    dog,
    cat,
    woodpecker;

var Animal = function(age, name, sound, region) {
    this.age = age || -1;
    this.name = name || "No name";
    this.sound = sound || "None";
    this.region = region || "Earth";
};

Animal.prototype.say = function() {
    console.log(this.name + " says: " + this.sound);
};

function inheritPrototype(subType, superType) {
    subType.prototype = Object.create(superType.prototype);
    subType.prototype.constructor = subType;
    subType.prototype.goAway = function() {
        console.log(this.name + " goes away and says " + this.sound);
    };
}

var Dog = function(age, name, region) {
    Animal.call(this, age, name, "woof", region);
};
inheritPrototype(Dog, Animal);

var Cat = function(age, name, region) {
    Animal.call(this, age, name, "meow", region);
};
inheritPrototype(Cat, Animal);

var Woodpecker = function(age, name, region) {
    Animal.call(this, age, name, "took", region);
};
inheritPrototype(Woodpecker, Animal);

function duckTyping(obj) {
    var typeObject = "";
    if (obj.age && obj.name && obj.sound && obj.region && obj.say) {
        if (!obj.goAway) {
            typeObject = "Animal";
        } else {
            switch (obj.sound) {
            case "woof":
                typeObject = "Dog";
                break;
            case "meow":
                typeObject = "Cat";
                break;
            case "took":
                typeObject = "Woodpecker";
                break;
            }
        }
    } else {
        typeObject = "Unknown";
    }
    return typeObject;
}
function duckTypingMethod() {
    var typeObject = "";
    if (this.age && this.name && this.sound && this.region && this.say) {
        if (!this.goAway) {
            typeObject = "Animal";
        } else {
            switch (this.sound) {
            case "woof":
                typeObject = "Dog";
                break;
            case "meow":
                typeObject = "Cat";
                break;
            case "took":
                typeObject = "Woodpecker";
                break;
            }
        }
    } else {
        typeObject = "Unknown";
    }
    return typeObject;
}

animal = new Animal(3, "Crocodile Gena", "Cheburashka! Come to me", "Zoo");
dog = new Dog(4, "Barsik", "Ukraine");
cat = new Cat(1, "Liesel", "Germany");
woodpecker = new Woodpecker(2, "Picker", "Japan");

console.log("Method call 'say':");
animal.say();
dog.say();
cat.say();
woodpecker.say();

console.log("Checking the object type with the function 'duckTyping':");
console.log(duckTyping(animal));
console.log(duckTyping(dog));
console.log(duckTyping(cat));
console.log(duckTyping(woodpecker));
console.log(duckTyping({}));  

console.log("Checking the object type with the function 'duckTypingMethod':");
console.log(duckTypingMethod.apply(animal));
console.log(duckTypingMethod.apply(dog));
console.log(duckTypingMethod.apply(cat));
console.log(duckTypingMethod.apply(woodpecker));
console.log(duckTypingMethod.apply({}));  

Related Tutorials