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 trialKosta Kuts
15,894 PointsWhy if I try to use method instead of getter Node returns the whole thing back?
I'm a bit confused about the result of my little experiment. I tried to use method instead of getter because honestly, I don't see the difference. But apparently here's the difference. Can someone explain to me why it returns the whole method instead of the result? What's wrong with using a method for this particular task? There's this line when you should use a getter instead of method?
here's the result of this script in console:
treehouse:~/workspace$ node students.js
31
Level getter: Sophomore
Level method: levelMethod(){
var level;
if ( this.credits > 90 ) {
level = 'Senior';
} else if ( this.credits > 60 ) {
level = 'Junior';
} else if ( this.credits > 30) {
level = 'Sophomore';
} else { level = 'Freshman'; }
return level;
}
class Student {
constructor(gpa, credits){
this.gpa = gpa;
this.credits = credits;
}
get level(){
var level;
if ( this.credits > 90 ) {
level = 'Senior';
} else if ( this.credits > 60 ) {
level = 'Junior';
} else if ( this.credits > 30) {
level = 'Sophomore';
} else { level = 'Freshman'; }
return level;
}
levelMethod (){
var level;
if ( this.credits > 90 ) {
level = 'Senior';
} else if ( this.credits > 60 ) {
level = 'Junior';
} else if ( this.credits > 30) {
level = 'Sophomore';
} else { level = 'Freshman'; }
return level;
}
stringGPA() {
return this.gpa.toString();
}
}
const student = new Student(3.9, 31);
console.log( student.credits);
console.log('Level getter: ' + student.level);
console.log('Level method: ' + student.levelMethod);
1 Answer
KRIS NIKOLAISEN
54,971 PointsTry:
console.log('Level method: ' + student.levelMethod());
()
invokes the method. Without ()
the definition is returned.
Kosta Kuts
15,894 PointsKosta Kuts
15,894 Pointshey Kris, thanks for the response. Does that mean that get method call do not require parenthesis?