Skip to content
scrambled
Programming Language Theory Flashcard

How to implement classes with prototypes?

Books Concepts Deckcompiler implementation

Question

How to implement classes with prototypes?

Answer

Create a prototype object that contains all the shared methods and default properties.

When you need an instance, create a new empty object and link it to that prototype so it delegates property lookups there.

Add a factory function or constructor that creates these linked objects and initializes their individual properties.

For inheritance, create a child prototype that itself delegates to the parent prototype, forming a chain.

Instances of the child delegate to the child prototype, which in turn delegates to the parent prototype, giving access to methods from both.