javascript - ES6: Call instance functions by their names -


i have next class in many function in :

class eventutils {      constructor(pid) {} bindevent1{} bindevent2() ... } 

i have run functions. before es5 used something this or this methods

however, after rewriting es6 classes examples not working more. had tried next code:

let eventutils = new eventsutils();    object.getownpropertynames(eventutils.__proto__).foreach((name) => {       if (name.indexof('bind') > -1) {                                                 let fn = eventutils[name];         if (typeof fn === "function") fn.apply(null);          }    }); 

but in such way scope of this not defined in applied function. right way such coding?

refering proto 1 of worst thing can ( either not work or 'll kill optimizations) , maybe simple loop can help:

for(let key of object.getownpropertynames(object.getprototypeof(eventutils))){  if(key.includes("event") && typeof eventutils[key] === "function"){    eventutils[key]();  } } 

however, dynamic variable names bad idea...


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -