Calling a function within a Class method?

Calling a function within a Class method?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Function Visibility and Scope in Class Methods

The core issue in your example stems from a fundamental misunderstanding of how JavaScript handles function scope within class methods. You have defined bigTest and smallTest as local function declarations inside the newTest method. While this is syntactically valid, it creates a specific execution context: these functions are inaccessible from outside that method.

When you attempt to call this.bigTest() or simply bigTest() from within scoreTest, the JavaScript engine looks for them in the current scope chain. Since newTest is not the constructor of the class and does not return an object containing these properties, they remain hidden inside the closure of the newTest function. They are effectively "orphaned" functions that exist only while newTest is executing.

To make these helper functions callable from other methods like scoreTest, you must change how they are defined. There are two primary approaches to solving this, each with distinct implications for your code structure.

Defining Methods on the Prototype or Instance

The most standard approach in modern JavaScript is to define all methods directly on the class body. By moving bigTest and smallTest outside of newTest, they become properties of the class prototype (or the instance itself, depending on your language version).

class test {
  bigTest() {
    // Big Test Here
    console.log('Running big test');
  }

  smallTest() {
    // Small Test Here
    console.log('Running small test');
  }

  constructor() {
    // Initialization logic
  }

  scoreTest() {
    // Calling the functions directly
    this.bigTest();
    this.smallTest();
    
    // Scoring code here;
  }
}

In this configuration, bigTest and smallTest are instance methods. When you instantiate the class with const obj = new test(), these functions attach to the specific instance (obj). Inside scoreTest, you access them using this.bigTest() because they are part of the same object's method chain. This pattern is generally preferred for readability and aligns with how most developers expect to interact with classes.

The Closure Approach

If your requirement is specifically that these functions must be generated dynamically inside newTest (perhaps based on runtime data), you can return an object containing them from the method. However, looking at your snippet, newTest appears to be a constructor-like function rather than a static factory method.

If newTest is intended to initialize the instance, it should likely be part of the constructor or simply contain logic that sets up properties. If you keep bigTest inside newTest, you must ensure newTest returns an object or modifies this such that these functions become properties of the resulting instance.

function newTest() {
  const self = this; // Capture context if needed later
  
  function bigTest() {
    // Big Test Here
  }
  
  function smallTest() {
    // Small Test Here
  }
  
  // Attach to the instance so other methods can find them
  self.bigTest = bigTest;
  self.smallTest = smallTest;
}

class test {
  constructor() {
    newTest.call(this); // Ensure 'this' is bound correctly
  }
  
  scoreTest() {
    this.bigTest(); // Now accessible
  }
}

Trade-offs and Best Practices

While defining functions inside another function creates closures that capture local variables effectively, it often leads to "fat" constructors or methods. This can make the code harder to read and maintain because the internal structure of the class is obscured by nested scopes.

By flattening the architecture—placing all public and private methods directly in the class definition—you leverage the language's native support for method visibility. If you need these functions to be private, modern syntax allows you to prefix them with #, preventing external access entirely while keeping them within the class scope.

class test {
  #bigTest() {
    // Private helper
  }
  
  scoreTest() {
    this.#bigTest(); // Accessible via # inside the class
  }
}

Choosing between nested functions and standard methods depends on whether you need the encapsulation of closures or the clarity of a flat method list. For most application logic, standard methods provide the necessary scope without the overhead of manual property assignment.

Our network