Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript Working with Classes in JavaScript Adding Methods To Our Class

I don't quite understand why do we need to use the method here in the class syntax?

class Pet { constructor(animal, age, breed, sound) { this.animal = animal; this.age = age; this.breed = breed; this.sound = sound; }

speak() { console.log(this.sound); }

class Student { constructor(gpa){ this.gpa = gpa; } stringGPA() { return this.gpa = "gpa"; } } const student = new Student (3.9);

What happens if we don't use the method?

Blake Larson
Blake Larson
12,932 Points

Are you asking why we use the new Student(3.9) syntax?

Mark Casavantes
Mark Casavantes
2,866 Points

Hello Hanwen,

Functions or methods are used to break your code into smaller parts. When you write a large program you can work on one method at a time to make sure it works properly.

If you do not use methods, you just have one program that does everything.

I hope this is helpful to you.