t1a13-Classes-Innes.html

What Are JavaScript Classes?

Good reference to JS classes

Classes are templates for creating objects. They can contain variables (called properties) and functions that work with those variables (called methods).

Classes connect to the topics of Objects and Functions.

A constructor() initializes each new object, while this refers to the particular object being used.

Example: Working Classes Code

Enter an animal's name and age, then create an instance of the myAnimal class.





Your animal will appear here...

Example: Class Methods

Methods are functions inside a class. They can use and modify an object's properties.

Create an animal above to use these methods.

Example: Creating Multiple Objects

One class can create many objects. Each object has its own properties, even though they share the same class template.

Click the button to create two animals.

What Did We Learn?

Remember: a class defines the structure and behaviour; an instance is an individual object created from that class.

Challenge: Add a new class

Create a myVehicle class with a constructor, at least two properties, and a method that displays its details. Create two separate vehicle objects and show that their properties are independent.