A.9. Classes and inheritance

Although ES5 supports object-oriented programming and inheritance, ES6 classes make the code easier to read and write.

In ES5, objects can be created either from scratch or by inheriting from other objects. By default, all JavaScript objects inherit from Object. This object inheritance, prototypal inheritance in this case, is implemented via a special property called prototype, which points at an object’s ancestor. In ES5, to create an NJTax object that inherits from the object Tax, you can write something like this:

function Tax() {
  // The code of the tax object goes here
}
 
function NJTax() {
  // The code of New Jersey tax object goes here
}
 
NJTax.prototype = new Tax();   1
 
var njTax = new NJTax();
  • 1 Inherits NJTax ...

Get TypeScript Quickly now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.