Skip to main content

Class instance methods


The getFullName() is an instance method. We write it inside the class definition, and then we are able to call it on an instance (for example the variable person).

You cannot call Person.getFullName(). For that to work, getFullName() must be a static method. This is explained in the next chapter.

It only works on instances - see the bottom two lines :)

// class definition
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

getFullName() { //this is the instance method.
return `${this.firstName} ${this.lastName}`;
}
}

// class usage
const person = new Person("Solo", "Stroup");
console.log(person.getFullName()); // "Solo Stroup"

Accessing Instance Variables


Here's an example of what not to do:

class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

getFullName() {
// ❌ this does NOT work
return `${firstName} ${lastName}`; //this, no pun intended, isn't a this.firstName.
}
}

Remember, the firstName and lastNames are only available inside the constructor, can be created using the this.firstName to be passed to other instances methods. NOTE: This is what the keyword this refers to. It's referring to the current instance of the class.

Read through this and understand the different ways you can usethis.

// class definition
class Course {
constructor(name, isCompleted) {
this.name = name;
this.isCompleted = isCompleted;
}

getDescription() {
if (this.isCompleted) {
return `You have completed the ${this.name} course.`;
} else {
return `You are currently studying the ${this.name} course.`;
}
}
}

// Sample usage - do not modify
const course1 = new Course("Learn JavaScript", false);
console.log(course1.getDescription()); // "You are currently studying the Learn JavaScript course"
const course2 = new Course("Learn Programming", true);
console.log(course2.getDescription()); // "You have completed the Learn Programming course"

Build your User Class


Write a User class with 2 instance methods: getFullName and canVote.

getFullName should return the prefix (for example, Mrs) followed by a dot character (.), followed by the full name. Check out the sample usage for examples. canVote() should return true when the age is 18 or above, and false otherwise.

// Start here :)
class User {
constructor() {

};
getFullName() {

};
canVote() {

};
};

//write in the rest of the pieces
class User {
constructor(fName,lName, prefix, age) {
this.prefix = prefix;
this.fName = fName;
this.lName = lName;
this.age = age

};
getFullName() {
return `${this.prefix}. ${this.fName} ${this.lName}`
}
canVote() {
if (this.age >= 18)
{
return true;
}
return false;
}
};


// Sample usage - do not modify
const user1 = new User("Sam", "Doe", "Mrs", 20);
console.log(user1.getFullName()); // "Mrs. Sam Doe"
console.log(user1.canVote()); // true
const user2 = new User("Alex", "Green", "Mr", 17);
console.log(user2.getFullName()); // "Mr. Alex Green"
console.log(user2.canVote()); // false

Object Oriented Programming


With OOP, you can describe the world with Classes and then instantiate (create) objects from the classes. Grouping is the one advantage of using classes but it's not the only benefit. OOP has additional features such as getters, setters, static methods, inheritance, and more.

You can call other instance methods from within the instance.

class User {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

getFullName() {
return `${this.firstName} ${this.lastName}`;
}

getGreeting() {
const fullName = this.getFullName(); // call an instance method from within
return `Hello ${fullName}`
}
}

GetTodos App


export default class Todos {
// Do NOT modify the constructor
constructor() {
// we don't capture any parameters here
// we're defining an array of todos with two example todos
this.todos = [{
title: "Learn JavaScript",
category: "work"
}, {
title: "Meditate",
category: "personal"
}];

}

// TODO: define remaining instance methods
// getAll
getAll() {
return this.todos;
};
getCount() {
return this.todos.length;
};
add(title,category) {
//my solution
this.todos.push({title: title,category: category});
return this.todos;
// the correct solution:
// this.todos.push({title, category});

};
getWork() {
return this.todos.filter( todo => todo.category === "work")

};
getWorkCount() {
return this.getWork().length;

};
getPersonal() {
return this.todos.filter( todo => todo.category === "personal")
};
getPersonalCount() {
return this.getPersonal().length;
};
}

Chapter Recap


  • Instance methods are functions that can be called on an instance of a class.
  • To be able to use instance variables inside an instance method, you have to prefix them with this (as long as they were captured in the constructor)
  • Inside an instance method, this refers to the current instance of the class.
  • Object-oriented programming (OOP) is when you describe the real world with classes (that you can then instantiate, which creates objects).
  • An instance method can call another instance method using the this.functionName() syntax.