Posts Tagged ‘design patterns’

Pass that Interview 1: Mimic a Class in Javascript

February 10th, 2010

Javascript, unlike most object-oriented programming languages, does not have the concept of Classes. Instead, Javascript uses a model in which objects are created, cloned, and enhanced by creating copies of the objects (it's loosely based on the Prototype pattern). There is a lot of power in this Prototypal system and people like Douglas Crockford have shown how Prototypal inheritance can be implemented in Javascript. This type of work is really cool; unfortunately it's not generally well-known or well-understood and many developers wish to use Javascript to simulate the more familiar Classes & objects as seen in languages like Ruby and Java.

Due to this simulation being common practice, the following has become a fairly common interview question:

How do you create a class in Javascript?

The answer is a bit wonky; but pretty simple to follow:

» Read more: Pass that Interview 1: Mimic a Class in Javascript