{"id":320614,"date":"2020-09-28T13:42:50","date_gmt":"2020-09-28T20:42:50","guid":{"rendered":"https:\/\/css-tricks.com\/?p=320614"},"modified":"2020-10-29T13:57:05","modified_gmt":"2020-10-29T20:57:05","slug":"the-flavors-of-object-oriented-programming-in-javascript","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/the-flavors-of-object-oriented-programming-in-javascript\/","title":{"rendered":"The Flavors of Object-Oriented Programming (in JavaScript)"},"content":{"rendered":"\n
In my research, I’ve found there are four approaches to Object-Oriented Programming in JavaScript:<\/p>\n\n\n\n
Which methods should I use? Which one is “the best” way? Here I’ll present my findings along with information that may help you decide which is right for you.<\/p>\n\n\n\n
To make that decision, we’re not just going to look at the different flavors but compare conceptual aspects between them:<\/p>\n\n\n\n\n\n\n\n
this<\/code><\/a><\/li>- Classes vs. Factory functions \u2013 Event listeners<\/a><\/li><\/ol>\n<\/div><\/div>\n\n\n\n
Let’s start with a foundation of OOP in JavaScript.<\/p>\n<\/div><\/div>\n\n\n
What is Object-Oriented Programming?<\/h3>\n\n\n
Object-Oriented Programming is a way of writing code that allows you to create different objects from a common object. The common object is usually called a blueprint<\/em> while the created objects are called instances<\/em>.<\/p>\n\n\n\nEach instance has properties that are not shared with other instances. For example, if you have a Human blueprint, you can create human instances with different names.<\/p>\n\n\n\n
The second aspect of Object-Oriented Programming is about structuring<\/em> code when you have multiple levels of blueprints. This is commonly called Inheritance or subclassing. <\/p>\n\n\n\nThe third aspect of Object Oriented Programming is about encapsulation<\/em> where you hide certain pieces of information within the object so they’re not accessible.<\/p>\n\n\n\nIf you need more than this brief intro, here\u2019s an article<\/a> that introduces this aspect of Object-Oriented Programming if you need help with it.<\/p>\n\n\n\nLet’s begin with the basics \u2014 an introduction to the four flavors of Object-Oriented Programming.<\/p>\n\n\n
The four flavors of Object-Oriented Programming<\/h3>\n\n\n
There are four ways to write Object-Oriented Programming in JavaScript. They are:<\/p>\n\n\n\n
\n- Using Constructor functions<\/a><\/li>
- Using Classes<\/a><\/li>
- Using Objects Linking to Other Objects (OLOO)<\/a><\/li>
- Using Factory functions<\/a><\/li><\/ol>\n<\/div><\/div>\n\n\n
Using Constructor functions<\/strong><\/h4>\n\n\nConstructors are functions that contain a this<\/code> keyword.<\/p>\n\n\n\nfunction Human (firstName, lastName) {\n this.firstName = firstName\n this.lastName = lastName\n}<\/code><\/pre>\n\n\n\nthis<\/code> lets you store (and access) unique values created for each instance. You can create an instance with the new<\/code> keyword.<\/p>\n\n\n\nconst chris = new Human('Chris', 'Coyier')\nconsole.log(chris.firstName) \/\/ Chris\nconsole.log(chris.lastName) \/\/ Coyier\n\nconst zell = new Human('Zell', 'Liew')\nconsole.log(zell.firstName) \/\/ Zell\nconsole.log(zell.lastName) \/\/ Liew<\/code><\/pre>\n\n\nClass syntax<\/strong><\/h4>\n\n\nClasses are said to be the “syntactic sugar” of Constructor functions. As in, Classes are an easier way of writing Constructor functions.<\/p>\n\n\n\n
There’s serious contention about whether Classes are bad (like this<\/a> and this<\/a>). We’re not going to dive into those arguments here. Instead, we’re just going to look at how to write code with Classes and decide whether Classes are better than constructors based on the code we write.<\/p>\n\n\n\nClasses can be written with the following syntax:<\/p>\n\n\n\n
class Human {\n constructor(firstName, lastName) {\n this.firstName = firstName\n this.lastName = lastName\n }\n}<\/code><\/pre>\n\n\n\nNotice the constructor<\/code> function contains the same code as the Constructor syntax above? We need to do this since we want to initialize values into this<\/code>. (We can skip constructor<\/code> if we don’t need to initialize values. More on this later under Inheritance<\/a>).<\/p>\n\n\n\nAt first glance, classes seem to be inferior to constructors \u2014 there’s more code to write! Hold your horses and don’t form a conclusion at this point. We have a lot more to cover. Classes begin to shine later.<\/p>\n\n\n\n