JavaScript is a prototype-based language. This is an unusual characteristic which means that under-the-hood it differs dramatically from traditional, object-oriented languages like Python, Ruby, and Java which use classes and inheritance. JavaScript strictly speaking has neither. No classes. No traditional inheritance. In JavaScript, functions are also objects, which means they have properties. And every function has a special property called prototype which is itself an object.

In this tutorial we’ll explore how Prototypal inheritance works in JavaScript and why it is important.

Object-Oriented Inheritance

In a traditional object-oriented language like Python classes serve as a blueprint, or set of instructions, for creating new objects which are instances of the class.

Here is how you would create a new class Dog and then instantiate it (create a new object based off Dog's instructions).

class Dog:
    def bark(self):
        print('Woof!')

d = Dog()
d.bark() # Woof!

Our class Dog contains a single method bark which outputs the string Woof!. When we create a new object d based on Dog, it automatically has access to all of Dog’s functionality. This is why we can call the method bark() on d successfully.

We can also add data that is automatically added whenever a new object is instantiated. In Python this is called the __init__ method.

One of the main reasons we jump through all these hoops rather than just create a simple Dog function in the first place is because we can now use inheritance with our classes. This lets us reuse code and not repeat ourselves.

Let’s write a program make an animal zoo. We’ll create an Animal parent class and then have Dog be a child that inherits from it. Here’s what the code would look like.

class Animal:
    """Represents any animal."""
    def __init__(self, name):
        self.name = name

    def sayHi(self):
        print('Hi I am a f'{self.name}')

class Dog(Animal):
    """Represents a Dog."""
    def __init__(self, name, breed):


There is also a variable--called `self` in Python--automatically included in all instances that references the newly created object itself.




The phrase "prototypal inheritance" is often used in JavaScript discussions as a jumping off point for


You'll often hear that JavaScript has "prototypal inheritance", which really means it


## What is inheritance?
Class-based languages like Java, C++, Ruby, or Python  where classes in



In classical inheritance, classes inherit from other classes which can be thought of as a blueprint. In prototypal inheritance, objects inherit from other object instances, which is like inheriting from an actual building.


JavaScript uses prototypal inheritance



In JavaScript, all functions are also objects.


```javascript
var obj = {};

Chrome console

Function Prototypes

In JavaScript, all functions are also objects, which means they can have properties. And they all have the prototype property, which is itself also an object:

function foo() {

}

// typeof foo.prototype // 'object'

Constructors

Any time you see the new keyword, it means the function is being used as a constructor:

var max = new Cat();

Now we have max, who’s a Cat.

By the way, by convention constructor names are Capitalized.

Methods

A method is just a function attached to an object. If we want to add a “meow” method to our Cat we’d do as follows:

```javascript function Cat() {

}

Cat.prototype.talk = function() { console.log(‘meow’); };

All functions automatically are initialized with a prototype object and in the above we added a function called talk function on.




Want to improve your JavaScript? I have a list of recommended JavaScript books.