3.2. InheritanceΒΆ

Javascript also supports inheritance. Here is a common example from Python.

In Javascript, the only syntax addition for implementing inheritance is to use the extends keyword as we do in the example below.

In older javascript code you may see something like:

function Dog(name)  {
    this._name = name;

Dog.prototype = new Animal()

Dog.prototype.speak = function() {

The assignignment to the prototype attribute of dog sets up the prototype chain so that if a function is not explicitly defined on the prototype object of Dog it will be found on the prototype object of Animal. In some ways this makes it more explicit with respect to how inheritance works, but at the expense of being more verbose, and different from most other languages.

You have attempted of activities on this page