How can I call a function within a class?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

In a class, functions are called methods, and you can call them in two main ways:

  1. Using an instance of the class (for instance methods).
  2. Directly from the class (for static methods).

Here’s how you can call a function (method) within a class in different scenarios:

1. Calling an Instance Method

Instance methods require an object (instance of the class) to call them because they typically operate on the instance's data.

Example:

class Person { constructor(name, age) { this.name = name; this.age = age; } // Instance method greet() { console.log(`Hi, my name is ${this.name} and I'm ${this.age} years old.`); } } // Create an instance of the class const person = new Person('Alice', 30); // Call the instance method using the instance person.greet(); // Output: Hi, my name is Alice and I'm 30 years old.

2. Calling a Static Method

Static methods belong to the class itself and do not require an instance to call them. They are defined using the static keyword and cannot directly access instance properties or methods.

Example:

class MathUtils { // Static method static add(a, b) { return a + b; } } // Call the static method directly on the class console.log(MathUtils.add(3, 5)); // Output: 8

3. Calling Another Method Within the Same Class

You can call another method within the same class using this (for instance methods) or the class name (for static methods).

Example:

class Calculator { // Instance method multiply(a, b) { return a * b; } // Instance method calling another instance method square(num) { return this.multiply(num, num); // Calling multiply() using `this` } } // Create an instance of the class const calc = new Calculator(); // Call the methods console.log(calc.multiply(3, 4)); // Output: 12 console.log(calc.square(5)); // Output: 25

For static methods:

class MathUtils { // Static method static square(num) { return MathUtils.multiply(num, num); // Calling another static method } // Another static method static multiply(a, b) { return a * b; } } // Call the static methods console.log(MathUtils.square(4)); // Output: 16

4. Private Methods (Optional in Modern JavaScript)

You can define private methods using the # prefix, which can only be accessed within the class.

Example:

class BankAccount { constructor(balance) { this.balance = balance; } // Private method #validateAmount(amount) { return amount > 0; } // Public method calling the private method deposit(amount) { if (this.#validateAmount(amount)) { this.balance += amount; console.log(`Deposited: $${amount}`); } else { console.log('Invalid amount'); } } } const account = new BankAccount(100); account.deposit(50); // Output: Deposited: $50 // account.#validateAmount(50); // Error: Private field '#validateAmount' must be declared in an enclosing class

Summary

  1. Call instance methods using an instance: object.methodName().
  2. Call static methods using the class name: ClassName.methodName().
  3. Within a class:
    • Use this.methodName() for instance methods.
    • Use ClassName.methodName() for static methods.

This approach ensures clear and efficient access to class methods. For mastering object-oriented principles in JavaScript, explore Grokking Advanced Coding Patterns for Interviews on DesignGurus.io!

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Interpreting domain-specific jargon in niche technical interviews
How to become a coder?
What are the types of interview in system analysis and design?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.