Purpose of these empty functions?

function Animal() { }
Animal.prototype.eat = function() {
  console.log("nom nom nom");
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;

I’m just wondering about what the lines ‘function Animal() { }’ and ‘function Bird() { }’ are for when the functions are wrapping anything?

I’m specifically referring to this problem, but a lot of other problems in the OOP section of the JS course include it too - https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance

Greatly appreciate any insight!

Thanks <3

1 Like

Those empty functions are being used as constructor functions, or classes. Often, we’ll be defining properties in the new thing we create with a class, like a name or breed or age, but we don’t have to. In this case, the only reason for the constructor is to link into the prototype chain.

Ah! So if my understand is correct, the line is actually creating the object ‘Animal’ but leaving it without any properties or methods? But the object still exists, so that it can inherit prototypes?

Think of Animal as the pattern, the “blueprint” used to create each instance of Animal. Defining the function of telling Javascript what to do when we say

const octopus = new Animal();

The function simply defines the shape of that new thing, the instance of our constructor. An empty function creates an empty object (no properties) that is wired to the constructor’s prototype. Doing this, the instance can use any of those prototype methods or properties.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.