I ran into an interesting problem, at least I think it is interesting and a little annoying. I have a class, for this question I will keep it extremely simple ...
class Foo { static pageChange() { console.log('The page changed'); } }
Now I can access this with Foo.pageChange () without any problems, everything works as expected. The hard part and interesting bit comes when I try to access it dynamically. I have a separate object that tracks events and processes them when necessary. This applies to the Google visualization library, where I have a table, and the table has events associated with it. I have an object that is responsible for creating all of this from PHP output. This is one feature of the system, in a simple explanation you can do something similar in PHP ...
GoogleVisLibrary::renderChart( array( 'chartType' => 'table', 'chartData' => $this->chartData, 'chartOptions' => $this-chartOptions, 'events' => array( 'sort' => 'Foo.pageChange' ) );
now it will create a table and all that is good. The problem is accessing this static method in the Foo class in javascript. What I had before creating Foo in the class was as follows.
var Foo = { pageChange: function() { console.log('page changed'); } }
then in my event library handler this would look something like this.
for(var i = 0, l = events.length; i < l; i++) { window[events.className][events.fnName].apply(); }
and this will work fine, since Foo can be obtained through the ['Foo'] window, but when you use the class definition shown in the first code snippet, you can no longer access it from the super-global window, it simply displays "undefined" .
So, is there a way to access the static method in the class through a dynamic link, as you can, using a Foo object through a global window?
I hope this makes sense, and I explain it correctly. If any question does not make sense, please feel free to ask, I will try to explain better. Thank you in advance for any help you can give.
javascript ecmascript-6 class
Neglected Sanity
source share