ctor and new inheritance

by ozzymcduff

HTML

<div id="consolelog"></div>

JavaScript

// Declaring our Animal object
var Animal = function () {
    var self = this;
    
    self.name = 'unknown';

    self.getName = function () {
        return self.name;
    }

    return self;
};

// Declaring our Dog object
var Dog = function () {
    // Dog extends animal
    var self = this;
    Animal.call(self);
    // A private variable here        
    var private = 42;

    // overriding the name
    self.name = "Bello";

    // Implementing ".bark()"
    self.bark = function () {
        return 'MEOW';
    }  

    return self;
};


// -- Done declaring --

// Creating an instance of Dog.
var dog = new Dog();
// Proving our case
$("#consolelog").html(
    ["Is dog an instance of Dog? ", dog instanceof Dog, "\n",
    "Is dog an instance of Animal? ", dog instanceof Animal, "\n",
    dog.bark() +"\n", // Should be: "MEOW"
     'Should be: "MEOW"',
    dog.getName() +"\n", // Should be: "Bello"
     'Should be: "Bello"',
    dog.private +"\n", // Should be: 'undefined'
    "Should be: 'undefined'"
    ].join("\n<br>")
);