Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
TypeScript Design Patterns

You're reading from  TypeScript Design Patterns

Product type Book
Published in Aug 2016
Publisher Packt
ISBN-13 9781785280832
Pages 256 pages
Edition 1st Edition
Languages
Author (1):
Vilic Vane Vilic Vane
Profile icon Vilic Vane

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 what a clone method would do. So actually a constructor can play the role of a concrete factory in some way:

interface Constructor<T> { 
  new (): T; 
} 
 
function createFancyObject...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}