Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic JavaScript Prototype and Inheritance Example for Animals

Tags:

javascript

I'm trying to grasp OOP with JavaScript with very simple examples.

My goal is to create a class hierarchy with Animals as the example.

In a simplified animal hierarchy we may see something like this:

          Animal
            /\
    Mammal     Reptile
      /\          /\
  Human Dog  Snake Alligator 

I want to take this example and create classes within JavaScript. Here is my attempt. What can I do to make it better?

function Animal(name) {
    this.name = name;
    }

function Mammal() {
    this.hasHair = true;
    this.numEyes = 2;
    this.blood = "warm";
}

function Dog(breed) {
    this.breed = breed;
    this.numLegs = 4;
}

Dog.prototype = new Animal("Fido");
Dog.prototype = new Mammal();

var Fido = new Dog("Lab");

console.log(Fido.name); // returns undefined when i want it to return Fido
console.log(Fido.hasHair); // returns true as expected
console.log(Fido.breed); // returns lab as expected

What I would like to do is have dog extend the properties of Mammal and Animal since it is both but it is not working correctly. I am assuming because i'm calling dog.prototype = new Mammal() after new Animal() that it is overwriting the connection.

How do I properly write out these classes so that I can call all properties of their parent classes?

Thank you.

like image 834
sjmartin Avatar asked Apr 24 '15 04:04

sjmartin


People also ask

What is prototype in JS inheritance example?

In JavaScript, an object can inherit properties of another object. The object from where the properties are inherited is called the prototype. In short, objects can inherit properties from other objects — the prototypes.

What is the difference between prototype and inheritance in JavaScript?

The most important difference between class- and prototype-based inheritance is that a class defines a type which can be instantiated at runtime, whereas a prototype is itself an object instance.

What is prototype and prototype chain in JavaScript?

Every object in JavaScript has a built-in property, which is called its prototype. The prototype is itself an object, so the prototype will have its own prototype, making what's called a prototype chain. The chain ends when we reach a prototype that has null for its own prototype.

What is JavaScript prototype used for?

Whenever we create a JavaScript function, JavaScript adds a prototype property to that function. A prototype is an object, where it can add new variables and methods to the existing object. i.e., Prototype is a base class for all the objects, and it helps us to achieve the inheritance.


1 Answers

You want to make use of Prototypical Inheritance, which in Javascript is a bit awkward but powerful.

function Animal(name) {
   this.name = name;
}

// Example method on the Animal object
Animal.prototype.getName = function() {
    return this.name;
}

function Mammal(name, hasHair) {
    // Use the parent constructor and set the correct `this`
    Animal.call(this, name);

    this.hasHair = hasHair;
}

// Inherit the Animal prototype
Mammal.prototype = Object.create(Animal.prototype);

// Set the Mammal constructor to 'Mammal'
Mammal.prototype.constructor = Mammal;

Mammal.prototype.getHasHair = function() {
    return this.hasHair;
}

function Dog(name, breed) {
    // Use the parent constructor and set the correct `this`
    // Assume the dog has hair
    Mammal.call(this, name, true);

    this.breed = breed;
}

// Inherit the Mammal prototype
Dog.prototype = Object.create(Mammal.prototype);

// Set the Dog constructor to 'Dog'
Dog.prototype.constructor = Dog;

Dog.prototype.getBreed = function() {
    return this.breed;
}

var fido = new Dog('Fido', 'Lab');

fido.getName();  // 'Fido'
fido.getHasHair(); // true
fido.getBreed(); // 'Lab'

A good resource to OOP in Javascript can be found on Mozilla Developer Network

like image 184
DevShep Avatar answered Sep 27 '22 20:09

DevShep