Hello welcome to the section on object oriented programming here's what we'll be doing in this section
.
We're going to define what object oriented programming or O.P. is and revisit the new keyword and understand
it in quite a bit of depth.
We'll then use constructor functions to reduce code duplication and refactor our constructors using
call and apply.
If you haven't watched a series on the keyword this I highly recommend that you do that first.
Everything in these videos will build all of our knowledge of the key word this as well as call apply
and bind.
Here we go.
So what is object oriented programming.
Simply put it's a programming model based around the idea of objects and blueprints which create objects
we call these blueprints classes and the objects that we create from our classes are traditionally called
instances in object oriented programming.
We strive to make our classes abstract and modular so that we can reuse classes easily and share them
amongst all parts of an application.
Javascript does not have built in support for classes unlike languages like Python Ruby and Java.
We can mimic the behavior of classes by using Javascript has functions and objects before we see how
to use functions to mimic the behavior of water classes.
Let's see why this programming technique is quite useful.
Imagine for a second that you're an architect and you're tasked with building 4 houses.
Each house is going to have a number of bedrooms bathrooms as well as a number of square feet.
The first thing that might come to mind is let's make an object for each house.
We need to make.
Seems pretty reasonable but once we start having to create more than a few objects things get very repetitive
and tedious.
Let's refactor this code by reading a blueprint for what a house should look like.
Let's make a function that when used we'll construct a house object in javascript.
We call the special functions constructor functions.
Now let's see what our constructor function for a house looks like.
Right off the bat we see that the name of the function is capitalized.
This doesn't change anything about the function.
It's just best practice and convention so that other developers know that this is a constructor function
.
The next thing we see is that the keyword this is back.
Just when you thought you were done with it.
If you haven't wants that series please go back and do so.
Otherwise the rest of the series will be quite confusing in this function.
We're attaching properties onto the keyword this we're somehow hoping that when we call the function
a new object will be returned to us with the values specified in this function.
However something's missing here.
If we look at the first house variable we are setting it equal to the result of the house function being
called.
The problem is our house function is not returning anything.
Therefore the function will return undefined.
So how do we fix this issue.
The answer lies in another special keyword that will explore in the next video.
The new keyword.
See you there.