Create a class to define the blueprint for creating objects
Add methods to a class
Set properties on an instance of a class
Make an instance of each class customizable
Create methods to alter the properties of an instance
Make a class inherit attributes from a "parent class"
Create static properties for a class
Create a factory
Explain why we need classes
Sometimes we need to repetitively create new objects with the same attributes. Imagine we wanted to create multiple people. All people have the same basic attributes, so it would be great if we could create a blueprint for our person creation process.
Create a class to define the blueprint for creating objects
When creating a class, it's custom to capitalize the first letter of the variable, so we know it's a class. This follows customs in other programming languages.
class Person {
};
Now we can "instantiate" or create new objects using this class. We do this by adding the new keyword before calling the class name like a function.
const me = new Person();
const bob = new Person();
console.log(me);
console.log(bob);
console.log(typeof(me));
console.log(typeof(bob));
Add methods to a class
Right now, our object doesn't do anything. Let's have it do some stuff;
class Person {
greet() {
console.log("hi!");
}
}
const me = new Person();
me.greet();
These methods can of course take parameters:
class Person {
greet(otherPerson) {
console.log("hi " + otherPerson + "!");
}
}
const me = new Person();
me.greet("bob");
If we have multiple methods, don't put commas between them:
class Person {
greet(otherPerson) {
console.log("hi " + otherPerson + "!");
}
jump() {
console.log("weeee!");
}
}
const me = new Person();
me.greet("bob");
me.jump();
Set properties on an instance of a class
If we log the instances of our class, we'll see they don't have any properties:
class Person {
greet(otherPerson) {
console.log("hi " + otherPerson + "!");
}
jump() {
console.log("weeee!");
}
}
const me = new Person();
console.log(me);
Let's add some properties with a constructor function. This is a function that gets called once, each time an object is created:
class Person {
constructor() {
this.legs = 2;
this.arms = 2;
this.eyes = "blue";
this.hair = "blonde";
}
greet(otherPerson) {
console.log("hi " + otherPerson + "!");
}
jump() {
console.log("weeee!");
}
}
const me = new Person();
console.log(me);
Make an instance of each class customizable
Of course, our constructor function can take params which we can use to alter the properties of the object instantiated. This allows us to customize each instance:
class Person {
constructor() {
this.legs = 2;
this.arms = 2;
this.eyes = "blue";
this.hair = "blonde";
}
greet(otherPerson) {
console.log("hi " + otherPerson + "!");
}
jump() {
console.log("weeee!");
}
}
const me = new Person();
console.log(me);
Create methods to alter the properties of an instance
We can of course, alter the properties of an instance, after it is created:
me.hair = 'red';
console.log(me);
But it's a nice practice to define a method that will alter that:
Sometimes you want to define properties that pertain to the class as a whole, not the instance. This allows us to limit, somewhat, what the user of class can do.
class Person {
static eyeColors() {
return ["blue", "green", "brown"];
}
//rest of class definition here...
}
//more code...
const spiderman = new Hero("Peter Parker", 16, Person.eyeColors()[0], "black");
Create a factory
Sometimes we need to have a factory object that will generate other objects
The purpose of the factory is so it can control the creation process in some way
This is usually a single object that exists throughout the program that performs a set of functions