Coding the Example
Is JavaScript an OOP Language?
Some say that JavaScript isn't a true object-oriented language; The class
statement just adds a bit of glitter to prototypical inheritance that is already there (more on that in a bit). They claim that JavaScript classes are therefore not classes in the traditional sense.
JavaScript utilizes a concept called constructor functions
to define objects and features (properties). Constructors allow you to effectively create as many objects as you need. When a constructor function creates a new object instance, the core functionality isn't completely copied over to the new object. Unlike "classic" OO languages, functionality is linked via a reference chain known as a prototype chain (we'll discuss this in the next module). Let's put this into practice with some real JavaScript.
Basic Example
Let's first define a person with a normal function. Add this to your
.js
file:Let's call the function and create a new person.
Let's simplify this with a constructor function:
Constructor functions don't return anything or explicitly create an object, they basically just define properties. The
this
keyword basically means that the name value passed to the constructor call will be the same one used by both thename
property andgreeting()
method. Please note that constructor names typically begin with a capital letter to be more recognizable.Let's create some objects!
Here are the properties:
When calling the properties or methods you need to start with
person1
orperson2
. Thename
property andgreeting
methods have the same syntax, but need to be called separately. This is one of the reasons thethis
keyword is so important, so that each will use its own values and not the others.
Let's look back at the constructor calls.
The new
keyword tells the browser we are creating a new object instance, followed by the function with parameters in parentheses, and storing the result in a variable. We already know the function definition, so let's see what these objects look like now.
The "Sarah" object looks the same. Rather than calling similar functions like greeting()
multiple times, we can define them on the prototype(more on that later).
Creating our Finished Constructor
Here's a new constructor function
Let's create an object instance; put this code right below the constructor function.
Challenges - Optional
Create a few more objects just to get the hang of this concept.
Figure out how to output different gender pronouns with the
bio()
method.Figure out how to take in more than two interests in the constructor function.
Other Methods of Object Instance Creation
The Object Literal
A very common way of creating an object is an object literal.
The main idea is that you are creating the properties and giving them values at the same time.
Object literals take up less space, and are perfect if you aren't doing anything else with that object. Constructor Functions are sometimes better because you can create additional objects off of it, basically using it as a template.
The Object() Constructor
Let's store an empty object in the
person
variable.Fun fact, you can add properties/methods using either dot or bracket notation!
Let's combine the previous two steps and pass an object literal to the
Object()
constructor.
The create() method
You can create objects based on existing ones using the
create()
method.person2
has access to the properties/methods ofperson1
.
Resources
Last updated