OOPs, I Just Don’t Know How to FUNCTION
By: Jen Henry
Tags: Programming, JavaScript, Object Oriented, Functional Programming

OOPs, I Just Don’t Know How to FUNCTION

Source: globalnerdy.com

What Is Functional Programming?

Functional Programming is a paradigm where the structure of a program is created using primarily functions. It uses pure functions to return the same result whenever provided the same variable; this is due to its referential transparency. Referential transparency is defined whenever a user references a variable anywhere in a program, it will have the same value every time (A.K.A. immutable variables!). These functions also never have side effects…meaning the global variables wont change (another GREAT example if immutable variables!). Another property of programming is that functions are considered first class functions when they can be used as parameters for one function or use it as a return value for another function. This is considered function composition; reducing redundancy and allowing the program to be configurable. When using arrays, functional programming will create a new array to store data instead of manipulating existing variables for the desired return. Example:

let myArray = ['I', 'love', 'chocolate', 'frogs'];

let madeAString = myArray.join(' ');

console.log(madeAString);

// the join() function takes an array, joins

// all the array items together into a single

// string, and returns this new string

What Is Object Oriented Programming (OOP)?

OOP is exactly how it sounds, it organizes software around object instead of functions, using a key: value pair to store data. Variables and functions are brought together inside of an object. OOP is an example of an imperative programming model by minimizing variable use. Lets look at some key features of OOP:

Encapsulation is used for information hiding, allowing only parts of data to be seen from the outside. This prevents all objects in a program from automatically communicating with each other by placing objects inside of classes.

Abstraction only shows essential attributes of the data relative to what the program allows the user to see. This will prevent users from seeing unnecessary data stored inside of the class.

Polymorphism allows different classes the ability to use the same interfaces. It also allows objects to take many forms

Inheritance establishes a hierarchy within classes and objects, creating the dynamic of parent-class/subclass.

Classes create objects inside of them to help define a set of properties for the objects inside of them. Using constructors, objects can be created with a series of user or program input. Example:

class Rectangle {

constructor(height, width) {

this.height = height;

this.width = width;

}

}

Objects can contain both data and functions to manipulate data. Objects use Key: Value pairs to store data. Objects within the same class with contain the same keys (identifiers). Values inside of an object can be a string, array, integer, or boolean. Example:

var myCar = {

make: 'Ford',

model: 'Mustang',

year: 1969,

running: true

};

Should I Choose Just One?

NO!! Both are valid programming methods to use; one can be better suited than the other and it’s important to understand both ways of thinking!