JavaScript Constructors
Published in
1 min readNov 28, 2014
Common patterns
I mainly use two types of constructors:
- literal
- custom constructor functions
These can take many forms. You can find the ones I care to mention below.
Literal Constructor (broken up)
I am not in a hurry option.
Literal Constructor (in one statement)
I need a break soon option.
Custom Constructor Functions (standard)
Old school JavaScript option.
Custom Constructor Functions (using an extra reference)
Fancy developers choice. I don’t mind using extra complexity (anonymous function) and resources (extra reference) for apparently no reason.
Custom Constructor Functions (returning a new object)
Another level of fanciness when you don’t like using the default this object.
Custom Constructor Functions (returning a new object abbreviated)
Previous one abbreviated.
References: JavaScript Patterns by Stoyan Stefanou.