javascript - JS - Call function by name (in object notation) -
i want clean old code , optimize it, uses same code. (with different names of functions call)
i make easier example , no, don't write on game. example looks more comprehensible explaination of issue.
character.sleep(1); character.changename(name); character.useitm(1423);
easier example:
object.function(parameters)
target this:
myfunc(funcname,value) { character.{funcname}(value); } $('.btn_sleep') { myfunc('sleep','1'); } $('.btn_cname') { myfunc('changename','harold'); } $('.btn_uitem') { myfunc('useitem','1423'); }
first thought use eval(), because no user-input come near of functions. dislike idea because of performance lost.
then looked around alternatives , found window[] , new function() solution.
but dont idea how use it, when want dynamcially call function name in object-notation. (or in worser cases, when you've result if-condtion function, called object-notation.)
could help?
the best way know how dynamically call functions using bracket notation because allows set object path variable
function myfunc(funcname,value) { character[funcname](value); } myfunc('sleep','1');
Comments
Post a Comment