30 Days Of JavaScript

Day 12: Classes

Lesson 3: Instance methods

The real power of classes comes in with instance methods. Just like you can have properties in a class you can have functions. When a function is in a class it's called a method ... don't ask why, it just it 🤷‍♂️

The reason this is so great is that it lets us encapsulate potential code inside our class definition, so that it doesn't clutter up our application later down the line.

Let's say we want to find the area of our triangle.

We could do this:

or we could put that math inside the class and simply ask the triangle for its area.

This makes a lot of sense, the triangle's area is something it owns and it has all the information it needs to calculate it itself. So there's no need to have this as an external thing cluttering up our code, we can put it inside each triangle we create and simply ask any instance of a triangle what it's area is.

Pretty neat!

Outline

Go Pro?

If you upgraded to pro, sign in here

  • About
  • Blog
  • Privacy
Looking to email me? You can get me on my first name at allthecode.co