There are four ways to create a blueprint for Object Oriented Programming:
- Constructor Syntax
- Classes Syntax
- OLOO
- Factory Functions
Constructor Syntax
You learned the Constructor syntax in the previous lesson.
Letโs say you want to create a Human that contains a firstName and lastName. Hereโs what you can write:
// Creating a Constructorfunction Human(firstName, lastName) { this.firstName = firstName this.lastName = lastName}You can then create an instance with the new keyword.
// Initiating an instanceconst zell = new Human('Zell', 'Liew')
Class Syntax
We can create a class with the class keyword. Like constructors, we begin the class with a capital letter.
It looks like this:
class ClassName { constructor() { // Code to initialize instances }}Everything that goes into a Constructor function would go into a classโs constructor method.
If you want to create a Human class with a firstName and lastName property, youโll write it like this:
// Creating a Classclass Human { constructor(firstName, lastName) { this.firstName = firstName this.lastName = lastName }}You can then create an instance with the new keyword.
// Initializing an instanceconst zell = new Human('Zell', 'Liew')
Constructors vs Classes
Constructor and Class are the same thing. Behind the scenes, JavaScript converts Classes into Constructors. Thatโs why people say classes are syntactic sugars.
Even though Constructors look easier to use now (since thereโs less code), Classes become easier to use when Inheritance comes into play. (More on inheritance later).
OLOO Syntax
OLOO is an acronym for Objects Linking to Other Objects. This acronym is coined and made popular by Kyle Simpson.
The OLOO Syntax uses a JavaScript object as the blueprint. The most basic form of an OLOO syntax is an empty object:
const OLOO = { // ...}You need a method inside the blueprint to initialise instances. This method is often called init. But it can also be called constructor, start, or whatever you want.
In init, you would write the same code as if you were writing the Constructor syntax.
// Creating an OLOO blueprintconst Human = { init(firstName, lastName) { this.firstName = firstName this.lastName = lastName },}You create an instance with Object.create. This instance must then be initialized with the init function.
const zell = Object.create(Human)zell.init('Zell', 'Liew')
If you return this inside init, you can chain Object.create with the init method.
const Human = { init(firstName, lastName) { // ... return this },}
const zell = Object.create(Human).init('Zell', 'Liew')Factory Functions
A Factory Function is a function that
- Returns an object.
- The returned object doesnโt need the
newkeyword
The simplest factory function looks like this:
// Factory that returns an empty objectfunction Factory() { return {}}We can create the same Human blueprint by writing this:
// Creating a Factoryfunction Human(firstName, lastName) { return { firstName: firstName, lastName: lastName, }}You run the Factory Function to initialise the instance. (You donโt need new. You donโt need to call init either).
const zell = Human('Zell', 'Liew')
Which flavour to use?
Itโs still too early to decide which flavour you should use. Go through the rest of the module and youโll arrive at an answer.
Welcome! Unfortunately, you donโt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson