Can I have an array/map of method names which represent an ordinarily chainable method call
[['add'],['subtract']]
And have them build up the chain progmatticaly, can you provide some pseudo code?
Can I have an array/map of method names which represent an ordinarily chainable method call
[['add'],['subtract']]
And have them build up the chain progmatticaly, can you provide some pseudo code?
For further actions, you may consider blocking this person and/or reporting abuse
Shaher Shamroukh -
Manzoor Sofi -
Abhay Singh Kathayat -
Vijay Kumar Rachuri -
Top comments (4)
Something like metaprogramming?
There is an old trick we used at old work, which uses
Function.prototype.call
. You can use an object of Function class itself to call dynamic functions from string. linkThere is a stackoverflow page I have saved on same topic. There are few different tricks mention there, I have tried both
Function
class trick andHandler
class trick. I have not used thewindows
object trick because I feel it is exposing a lot of attack area to the windows object.Here is a working code
when function_to_run is 1, it will call console.warn, when it is 0, it will call console.log.
Ah it's so obvious now that you show me, eval type thing nice!!
According to MDN docs, its not exactly eval. Every function in js is an object of Function class, so this is just manual work to implement same thing.
One drawback is that this will create an object everytime for execution and mark it for garbage collection, so if you have a fix set of functions that just needs to be called dynamically, use conditional logic like `if ('add') { this.add()}