Intro to JavaScript Classes Extending Built-in Classes

Learning objective: By the end of this lesson, students will understand how extend JavaScript’s built-in classes

Extending JavaScript’s built-in classes

It’s possible to use extends to subclass JavaScript’s built-in classes.

For example, it would be nice to have a last property on arrays that returns the last element of the array:

class MyArray extends Array {
  get last() {
    return this[this.length - 1];
  }
}

You then would need to instantiate the MyArray class to create the specialized array:

const nums = new MyArray(1, 2, 3, 4, 5);
console.log(nums.last); //-> 5

All other array properties and methods are available due to inheritance!

Learn more about prototypal inheritance and the prototype chain here in the docs.