Skip to Content
Learn
Classes
Instance

Now, we’re ready to create class instances. An instance is an object that contains the property names and methods of a class, but with unique property values. Let’s look at our Dog class example.

class Dog { constructor(name) { this.name = name; this.behavior = 0; } } const halley = new Dog('Halley'); // Create new Dog instance console.log(halley.name); // Log the name value saved to halley // Output: 'Halley'

Below our Dog class, we use the new keyword to create an instance of our Dog class. Let’s consider the line of code step-by-step.

  • We create a new variable named halley that will store an instance of our Dog class.
  • We use the new keyword to generate a new instance of the Dog class. The new keyword calls the constructor(), runs the code inside of it, and then returns the new instance.
  • We pass the 'Halley' string to the Dog constructor, which sets the name property to 'Halley'.
  • Finally, we log the value saved to the name key in our halley object, which logs 'Halley' to the console.

Now you know how to create instances. In the next exercise, you will learn how to add getters, setters, and methods.

Instructions

1.

Create an instance of the Surgeon class — set the name to 'Curry' and department to 'Cardiovascular'.

Save the instance to a constant variable called surgeonCurry.

2.

Create an instance of the Surgeon class — set the name to 'Durant' and department to 'Orthopedics'.

Save the instance to a constant variable called surgeonDurant.

Folder Icon

Take this course for free

Already have an account?