Prototype

As JavaScript is a prototype-based programming language, you might be using prototype related patterns all the time without knowing it.

We've talked about an example in the Abstract Factory Pattern, and part of the code is like this:

class FreightRocketFactory 
implements RocketFactory<FreightRocket> { 
  createRocket(): FreightRocket { 
    return new FreightRocket(); 
  } 
} 

Sometimes we may need to add a subclass just for changing the class name while performing the same new operation. Instances of a single class usually share the same methods and properties, so we can clone one existing instance for new instances to be created. That is the concept of a prototype.

But in JavaScript, with the prototype concept built-in, new Constructor() does basically ...

Get TypeScript Design Patterns 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.