Now one of the first things about a class that's really great is the ability to add a constructor function. A constructor function is a special function, it is specific to the class, automatically fires, and lets you initialize the instance of your class. In this case, we want to do something to customize an individual person when a new Person is created.
To define a constructor function we start with the name, constructor, but instead of adding a colon or anything else, we simply go right to our function arguments and right into the curly braces:
class Person {
constructor () {
}
}
This is our function, it's just like a regular function. The code inside is going to get executed and the brackets are our arguments, but the syntax for setting it up does look pretty different than what we would do on an object or on anything else.
Now this constructor function gets called by default. You do not need to manually call it, and that actually gets called with the arguments that you specify right in Person, which means that we could have our Person constructor function take two arguments; maybe we want to initialize a new person with name and age. That means we would pass in name and age, I can say that name is a string, I'm going to set it as my name and age is a number like 25:
class Person {
constructor (name, age){
}
}
var me = new Person('Andrew', 25);
The constructor function is now going to get called with this data, and we can prove this by using console.log to print out the name, and as the second argument the age:
class Person {
constructor (name, age){
console.log(name, age);
}
}
var me = new Person('Andrew', 25);
Now let's go ahead and run this file and see what we get; it's sitting in server/utils. I'm going to shut down nodemon and run it using following command:
node server/utiles/users.js
When I run the file, we get Andrew 25 because the arguments were properly passed into the constructor:

Now passing data in really isn't useful, what we want to do is modify the specific instance. We want to set this person's name and age not the name and age for all people. In order to do that, we're going to use the this keyword. In class methods and in the constructor function, this refers to the instance as opposed to the class, which means we can set a property on this individual person, this.name = name, just like this:
class Person {
constructor (name, age) {
this.name = name;
}
}
And we can do the exact same thing for age, this.age = age:
class Person {
constructor (name, age) {
this.name = name;
this.age = age;
}
}
Using this is how we customize the individual instance. Now what we have is an object, and we can actually access those properties. The me variable we defined is identical to the this variable, which means that we can actually access those properties. We'll add console.log, I'm going to print the string this.name for formatting only, and then I'm going to reference the actual me.name property. And we can do the exact same thing for age; we're going to print what we had put in as this.age, only we're going to access it via me.age:
var me = new Person('Andrew', 25);
console.log('this.name', me.name);
console.log('this.age', me.age);
We can now rerun the file using, nodemon server/utils/users.js, and we get exactly what we'd expect:

The individual person was updated; this.name was set to Andrew and it is indeed showing. Now that we have a basic idea as to how we can initialize a class, let's go ahead and talk about methods.