Call non static method from static method javascript

I am curious as I get a "undefined is not a function" error. Consider the following class:

var FlareError = require('../flare_error.js');

class Currency {

  constructor() {
    this._currencyStore = [];
  }

  static store(currency) {
    for (var key in currency) {
      if (currency.hasOwnProperty(key) && currency[key] !== "") {

        if (Object.keys(JSON.parse(currency[key])).length > 0) {
          var currencyObject = JSON.parse(currency[key]);
          this.currencyValidator(currencyObject);

          currencyObject["current_amount"] = 0;

          this._currencyStore.push(currencyObject);
        }
      }
    }
  }

   currencyValidator(currencyJson) {
    if (!currencyJson.hasOwnProperty('name')) {
      FlareError.error('Currency must have a name attribute in the json.');
    }

    if (!currencyJson.hasOwnProperty('description')) {
      FlareError.error('Currency must have a description attribute in the json.');
    }

    if (!currencyJson.hasOwnProperty('icon')) {
      FlareError.error('Currency must have a icon attribute in the json.');
    }
  }

  static getCurrencyStore() {
    return this._currencyStore;
  }

};

module.exports = Currency;

Refactoring aside, the issue is on the line: this.currencyValidator(currencyObject); I get the error "undefined is not a function"

I assume this is because I have a static method who's internals call a non static method? Would that non static method have to be static? and if so does the concept of this.methodName still work?

asked Oct 27, 2015 at 15:17

2

No, a static method cannot call a non-static method.

Consider that you have objects a and b, both instances of Currency. currencyValidator exists on those two objects. Now store() belongs to the class Currency itself, not one of those objects. So, in Currency.store(), how does it know which object to call currencyValidator() on? The simple answer is it doesn't so it can't. This is one of the pitfalls of using static methods and one of the reasons people often urge against them.

Regardless, you can get around this by passing a into Currency.store(), and calling a.currencyValidator() instead.

answered Oct 27, 2015 at 15:23

JoshJosh

8,0093 gold badges25 silver badges48 bronze badges

0

It makes no sense to call a non-static function from a static one, in any language. Static (in this context) means that it's basically outside of the object, independent in all but name. It's not tied to any instance and thus there's no this or self to call non-static (ie member) fields.

answered Oct 27, 2015 at 15:20

Call non static method from static method javascript

BlindyBlindy

62.2k10 gold badges86 silver badges125 bronze badges

3

No, in general static methods cannot call instance methods. It makes little sense to be able to do so.

The only caveat is that there is nothing stopping a static method instantiating an instance of a class, at which point it can call the instance methods in the usual way.

answered Oct 27, 2015 at 15:21

JamiecJamiec

131k13 gold badges135 silver badges189 bronze badges

A static method provides NO reference to an instance of its class (it is a class method) hence, no, you cannot call a non-static method inside a static one.

Of course you may either

  • Create an object of the class inside the static method and then call the non-static method using such an object.

or

  • Pass an instance of the class as static method parameter (and then, again, use such instance for invoking the non-static method).

Due to the vagueness of your question there can be many other ways (for instance, calling a non-static method of an object of a completely unrelated class).

Providing examples is left as an exercise.

Can static method call non static method Javascript?

No, a static method cannot call a non-static method.

Can we call non static method from static?

The only way to call a non-static method from a static method is to have an instance of the class containing the non-static method. By definition, a non-static method is one that is called ON an instance of some class, whereas a static method belongs to the class itself.

Can we call non static method from main method?

Since you want to call a non-static method from main, you just need to create an object of that class consisting non-static method and then you will be able to call the method using objectname.

How do you call a non static method from a static method in Java in same class?

If we are calling a non static method then we need to use object so that it will call corresponding object non static method. Non static methods will be executed or called by using object so whenever we want to call a non static method from static method we need to create an instance and call that method.