Ecmascript 6 how to call a static class by reflection method

Therefore, I would call the method of the static class es 6 By reflection using the class name stringName and the method name string. I tried several ways. Unfortunately, I did not seem to find the correct path for this.

By the way (as mentioned in the comments below) I'm looking for a solution in which I get the class name and method name from the dom attributes so that they are a string.

Can anyone help?

class a{
	static b(nr){
  	alert('and the answer is' + nr)
  }
}

let aClassName = 'a',
		aMethodeName = 'b',
    theCompleteCall = 'a.b',
    anArgument = 42;

//Reflect.apply(theCompleteCall,anArgument);
//window[aClassName][aMethodeName](anArgument);
//window[theCompleteCall](anArgument);
Run code
+4
source share
2 answers

Because of that letand classare not declared in the global scope ( read more ), you need to declare your class in an accessible area, such as:

window.a = class a{
    static b(nr){
    alert('and the answer is' + nr)
  }
}

let aClassName = 'a',
        aMethodeName = 'b',
    theCompleteCall = 'a.b',
    anArgument = 42;

, :

window[aClassName][aMethodeName](anArgument)

, , .

+3

.

-2

Source: https://habr.com/ru/post/1693019/


All Articles