`In JavaScript strict mode, overridden function fails to execute`

I'm facing an issue where a modified function is not being called.

The original implementation is shown below.

// Base file defining and exporting a function
function a() {
    // do something
}

module.exports = {
    a: a,

    b: function () {
        $('body').on('some_event', function (e, response) {
            // do something
            
            // call a
            a();
        });
    }
}

The override looks like this.

// Overriding the function a()
var base = require('mystuff/base');

function a() {
    // overridden implementation
}

base.a = a;

module.exports = base;

When some_event occurs on the page, b() runs and ultimately calls a(), but it executes the base implementation rather than the overridden one.

How can I resolve this so that the overridden function is executed when the event is triggered? The goal is for b() to run and then execute the overridden a() in its final step.

Answer №1

Just a few tweaks are needed to improve the code. Below is a cleaned-up version with one key change being the update to the call of a():

Kindly note that these changes were made based on the OPs comment.


class Base {
  a() {
    console.log('in mystuff/base a()');
  }
  b() {
    setTimeout(this.a, 2000);
  }
}

You can test this out on this repl:

https://repl.it/@randycasburn/overriding-imports

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the solution for halting code execution in a foreach loop with nested callbacks?

Currently, I am in the process of setting up a nodejs database where I need to retrieve user information if the user exists. The issue I'm facing is that when I return callback(null) or callback(userdata), it does not stop the code execution and resul ...

Switching Between HTML Using Javascript

I am currently working on an app that allows users to easily check the local weather and temperature in either celsius or fahrenheit. However, I've encountered a problem when trying to switch between the two unit types by simply clicking on the temper ...

Prevent unchecking a checked list item by clicking on it

Is it possible to prevent the user from unchecking a list item once it has been checked? Your assistance is greatly appreciated! $(".collectioncontainer ul li").click(function(){ $('.collectioncontainer ul li.checked').not(this).removeClass( ...

Removing a dynamic TypeScript object key was successful

In TypeScript, there is a straightforward method to clone an object: const duplicate = {...original} You can also clone and update properties like this: const updatedDuplicate = {...original, property: 'value'} For instance, consider the foll ...

Every time I switch tabs in Material UI, React rebuilds my component

I integrated a Material UI Tabs component into my application, following a similar approach to the one showcased in their Simple Tabs demo. However, I have noticed that the components within each tab — specifically those defined in the render method ...

Creating an 8-bit grayscale JPEG image using HTML5 and encoding it from a canvas source

Is there a simple method to convert an 8-bit grayscale JPEG from Canvas using client side technologies in a web browser (such as HTML5, canvas, WebGL, and JavaScript)? Are there any pre-made JavaScript libraries available for use, without requiring extens ...

Is it possible to extract the version_name using the version_code of an android package?

Is it possible to retrieve the version_name by using the version_code of an Android package? For instance: 'com.nianticlabs.pokemongo' version_code: 2017121800 => version_name: 0.87.5 I'm looking for a function that can accomplish th ...

The component fails to update after a change in state

I'm currently working on a project that involves fetching videos from YouTube and displaying them on the browser. I have successfully retrieved the videos and updated the state property (confirmed using console log). However, I am facing an issue whe ...

AngularJS $http get isn't functioning properly, but surprisingly $.ajax works perfectly

Recently delving into the world of AngularJS, I am facing a hurdle with $http functionality. In my factory setup below: app.factory('employeeFactory', function ($http) { var factory = {}; // Retrieving data from controller var emplo ...

Navigating a roster of users in JavaScript

I'm dealing with a collection of user data stored in an array accountsade: [ { id: 0.4387810413935975, name: "Adrian", password: "345", userName: "nathanael" }, { id: 0. ...

Performing a callback function in node environment

It's common knowledge that Node.js operates on a single thread and relies on asynchronous operations for I/O tasks. When Node.js is not actively running code, it actively listens for events using platform-native APIs that allow it to respond to eve ...

When trying to access an array directly within an object, it all too often returns as undefined

There is a React Component that has been provided with an array of objects (main_object), one of which contains another array of objects (secondary_object). Strangely, when trying to print the main object in console.log, the array is visible, but attemptin ...

Determine the percentage between two different dates

I am looking to calculate the percentage difference between two dates, considering only hours for scaling. For example: 22-08-2017 09:00 as the start date, 30.09.2017 22:00 as the finish date, Today's date is 01.09.2017. When I check the system toda ...

What steps do I need to take to ensure the progress bar extends all the way to the end of the sn

I am currently facing a challenge where the progress bar line in my message queue does not reach the end of the message before it closes. I have included a brief video showcasing the issue along with the relevant code snippet. Any suggestions or ideas woul ...

Unable to showcase the worth

My code in the render method is aimed at accessing the value of arr[1].Title, however, it seems to be causing an error. public render(){ var arr; return ( { Array.apply(null, {length:this.state.length}).map((value, i) => {i*=6; return ...

Is there a way to transform integers into their matching strings within Vue markup?

I have a database table where I retrieve data. The "status" field is used to manage the information in this table. If the status is equal to 1, it indicates "Active." If the status is equal to 2, it signifies "Completed." If the status is equal to 0, it r ...

Creating Web Components using JavaScript on the fly

I tried to create web components directly from JavaScript, but I encountered an issue where the public constructor could not be found. Here's a basic example to illustrate the situation: The HTML Template: <polymer-element name="wc-foo" construct ...

Disable Bootstrap's CSS3 transitions for progress bars

I'm trying to find a way to stop Bootstrap's CSS3 transitions from animating on the progress bars when I update them using javascript/jquery. Does anyone have any suggestions? I attempted to use this method mentioned here but it wasn't succ ...

Which is quicker when utilizing jQuery selectors: selecting by .classname or div.classname?

Which is quicker: using $(".classname"). or adding the tag to search for as well? $("div.classname") In my opinion, using just the classname would be faster because jQuery can simply loop through all classnames directly, whereas in the second example i ...

Tips for updating the content within an HTML tag with jQuery

I am looking to update the parameter value of an applet tag based on the selection from a dropdown menu. As I am new to jQuery, I would appreciate any guidance on how to achieve this using jQuery. This is my current applet code: <applet id="decisiontr ...