tracks

Codecademy

Sign in Sign up
Ajax-loader-large-white
14/33
Introduction to Objects I
So What's a Method?

In the last section, we discussed properties. We can think of properties as variables associated with an object. Similarly, a method is just like a function associated with an object.

Let's look at bob, our same person object from the last lesson. Instead of just having the properties name and age (line 3 & 4), bob also has a method called setAge (line 6). As you can probably guess, this method sets bob's age to whatever argument you give it.

Notice how we define setAge kind of like we define a property. The big difference is that we put in a function after the equals sign instead of a string or number.

We call a method like a function, but we use ObjectName.methodName(). Look at line 10 where we use the method to change bob's age to 40. We did this by calling bob.setAge(40);.

Instructions

Try it yourself! Use the method setAge to set bob's age to 20.

?
Stuck? Get a hint! Hint

Remember, to call functions that take parameters we include them in parentheses. In this case we will call bob.setAge(someNewAge);, and input 20 as our new age.

script.js
Back to Editor

Sign in