Thursday 15 March 2012

node.js - Call function dynamically in Javascript -



node.js - Call function dynamically in Javascript -

i want dynamically phone call function string "user.find". script phone call function find() in object user if function exists. here's tried:

var user = {}; user.find = function(){ homecoming 1; } var input = 'user.find'; var some_data_array = {name: 'john doe'}; var method = input.tostring().split('.'); var nameobj = method[0].substring(0,1).touppercase() + method[0].substring(1); var methodtocall = method[1]; nameobj.call(methodtocall, some_data_array);

but returns:

nameobj.call(methodtocall, some_data_array); typeerror: object user has no method 'call'

any idea? can't utilize window since node.js problem, script not executed in browser.

you're misunderstanding call().

call() lets phone call method different this.

you want property name:

object[methodname](arg1, arg, ...);

javascript node.js

No comments:

Post a Comment