Consider a program where we represent a bunch of people who can introduce themselves. We could make a Person
class and give it a Method (a function) named greet()
class Person { constructor(name) { this.name = name; } greet() { console.log("Hello, my name is " + this.name); } }
We can now make some different Person
objects and have them greet()
.
var person1 = new Person("Jason"); var person2 = new Person("Ally"); person1.greet(); //Logs "Hi, my name is Jason" person2.greet(); //Logs "Hi, my name is Ally"
In our lessons we usually deal with drawing and moving lots of objects. To handle this we usually create a draw()
and a move()
method, that way we can tell all of our player's to .draw()
and .move()
.
Javascript games that can be created in one lesson
Use classes to make two pokemon objects who will battle
A walkthrough for creating a button class that can be used in any project you choose