Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript (2015) Introduction to Methods Different Kinds of Objects

Gerry K
Gerry K
5,877 Points

Clarify what is state and behaviour of an object

I don't fully understand what these two aspects of an object are and feel it wasn't well explained in the video so was hoping someone could clarify with an example of what these two things actually mean.

Ken Stone
Ken Stone
29,703 Points

for the radio example state is like on or off, the radio could be on so the state would be on behavior is what is does, it plays music, when the state is off the behavior is not playing music

another example could be a cow the state could be sleeping or awake when the state is awake the behavior could be eating grass

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Gerry K

Imagine a dog. This is an example of an object.

A state can be on or off. If it's asleep, its state is off. If it's awake, the state is on.

Behaviour: If the dog is awake, what is it doing? Examples of behaviour might be barking, sniffing, eating or drinking, pawing, jumping, playing, to name a few.

I hope my explanation makes sense.

1 Answer

In simple terms, the state of an object is its state at a certain point in time, and a behaviour of an object is the things it can do which is stored in methods. When the method is run, it performs an action, thus changing the state of the object.

State Examples:

  1. A radio can be "on" or "off"
  2. A song can be "playing" or "not playing"
  3. A task can be either "complete" or "incomplete"

Behaviour Examples:

  1. If a power button is pressed, the radio turns from "off" to "on"
  2. If a stop button is pressed, the song that is playing "stops"
  3. If you finish a task and click on the checkbox to the left, a line crosses thru the text task indicating it's complete