Object Oriented Programming in JavaScript
Before Object Oriented Programming came along, there was Procedural Programming. In Procedural Programming we have some functions to handle DOM manipulation, some functions handle server communication, and other functions may serve as helper functions to others — dividing a program into a set of functions. Data is stored in various variables and functions that are invoked using the data. This style of programming is how many developers start learning to code and, as we keep learning, we realize that the more complicated our code gets, the harder it is to understand and change it. When the program is larger with many interconnecting functions and dry code, changing one function can break code in another child or depending function. It becomes increasingly difficult to follow the flow your code as you build more.
Object Oriented Programming came to save the day. In Object Oriented Programming we combine a group of related variables and functions into a unit — which we call Encapsulation.
Get Nathan F.’s stories in your inbox
Join Medium for free to get updates from this writer.
A Person in Procedural Programming-
let name = 'Adam La Rosa';
let age = 15;
function sayHello(nameOfPerson) {
console.log(`Hello, my name is ${nameOfPerson}.`);
}
function sayAge(age) {
console.log(`I am ${age} years old.`);
}
sayHello(name);
sayAge(age);A Person in Object Oriented Programming -
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name}.`);
}
sayAge() {
console.log(`I am ${this.age} years old.`);
}
}
let adam = new Person('Adam La Rosa', 15);
adam.sayHello();
adam.sayAge();The End Results of both styles are the same, but the data is more structured in the second way because it’s all in one place and easier to change.








