Accessing Methods Inside Classes in OOP in JavaScript
Some methods can be called inside others via this. Let's look at an example. Let's say we have a class with a user and a method that returns a property:
class User {
show() {
return this.name;
}
}
Let's also have a method cape that converts the first letter of a string to capital:
class User {
show() {
return this.name;
}
cape(str) {
return str[0].toUpperCase() + str.slice(1);
}
}
Let's use the cape method inside the show method:
class User {
show() {
return this.cape(this.name);
}
cape(str) {
return str[0].toUpperCase() + str.slice(1);
}
}
Make a class Student with properties name and surn.
Create a helper method that will get the first character of the string and capitalize it.
Create a method that will return the student's initials, that is, the first letters of his first and last name.