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 trialBuster Siem
7,280 PointsIf-statements conditioning to a getter-property in a setter-property?
I can't crack how I am supposed to write this if statement. I read on MDN that you had to access the level-property with the self.-prefix, but I don't think the Treehouse-teacher has taught anything about this in the video... HELP, please.
class Student {
constructor(gpa, credits) {
this.gpa = gpa;
this.credits = credits;
}
stringGPA() {
return this.gpa.toString();
}
get level() {
if (this.credits > 90) {
return 'Senior';
} else if (this.credits > 60) {
return 'Junior';
} else if (this.credits > 30) {
return 'Sophomore';
} else {
return 'Freshman';
}
}
set major(major) {
if (self.level === 'Senior' || self.level === 'Junior') {
this._major = major;
} else {
this._major = 'None'
}
}
}
var student = new Student(3.9, 60);
1 Answer
Steven Parker
231,184 PointsCan you post a link to where you saw this? I'd be curious to see it!
But the word "self" has no special meaning in JavaScript. The appropriate prefix is "this.
", just like you used for accessing the backing property.
Buster Siem
7,280 PointsBuster Siem
7,280 PointsThanks, Steven! I just realize now as I read the MDN page again, that I might have misunderstood it. I was also wondering why it wasn’t in the video if it was this prefix to use. Haha.