Call Static Methods from Regular Es6 Class Methods

JS call static method from class

From MDN documentation

Static method calls are made directly on the class and are not
callable on instances of the class. Static methods are often used to
create utility functions.

For more please see=> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static

You can do something like this => this.constructor.staticMethod()); to call static method.

class StaticMethodCall {
constructor() {
console.log(StaticMethodCall.staticMethod());
// 'static method has been called.'

console.log(this.constructor.staticMethod());
// 'static method has been called.'
}

static staticMethod() {
return 'static method has been called.';
}
}

Pre ES6 Static Functions in a Class

How do I create static functions which can be called without an instance, and how do I assign static constants which can be accessed without an instance?

Both methods and constants are just properties of the class (constructor function) object, and are created by assignment:

var MyClassBase = function(str){
this.m_Data = str;
};
MyClassBase.STATIC_STRING = "Ooops";

See also JavaScript: Class.method vs. Class.prototype.method or Static variables in JavaScript.

When I create my class MyClass which inherits MyClassBase, then MyClass.STATIC_STRING is undefined. Is that normal class conventions?

Yes, this is normal in ES5 inheritance. To emulate ES6, you would need to manually set the prototype of the constructor:

Object.setPrototypeOf(MyClass, MyClassBase);

(or in ES5, one did use the now-deprecated __proto__).

Notice however that often enough you want to explicitly refer to MyBaseClass anyway.

How to call a JavaScript static class method when the class name and method name are strings?

Since the answer is buried in the comments to the question, I figured I should go ahead and post it here as an actual answer, so future viewers can easily find the answer.

The answer is: In JavaScript, it's not possible to invoke a method in a class that was loaded from an ES6 module, where the name of the class is in a string.

ES6 Javascript: Calling static methods from classes with arrow functions

Arrow functions inherit their this from the outer scope rather than their this depending on their calling context. Since staticMethod2 tries to access this.staticMethod, that will only work if this refers to ClassWithStaticMethod - that is, if staticMethod2 is a standard function, not an arrow function.

You also need to invoke this.staticMethod(). (const yee = this.staticMethod; will result in the staticMethod being coerced to a string, rather than being called)

Change those two issues, and it works as expected:

class ClassWithStaticMethod {
static staticMethod = () => { return ('staticMethod'); };
static staticMethod2 = function() { const yee = this.staticMethod(); return 'staticMethod2 '+yee; };
}
console.log(ClassWithStaticMethod.staticMethod2());

Calling static method from constructor es6

this.constructor.staticMethod()

can be used to avoid referring to the class directly (particularly useful for class inheritance and pasted code).



Related Topics



Leave a reply



Submit