Call method by variable


#1

I’m using validated methods. How can I use a variable for a method name?

if (type === 'tables') {
	tablesInsert.call({ title: title });
}
else if (type === 'articles') {
	articlesInsert.call({ title: title });
}

I tried using

window[type + 'Insert'].call({});

But this gives me window.method.call is not a function


#2

Hello.

If you encounter this problem on the server side, you can try gobal[type+‘Insert’].call({});
I think it is the equivalent of window.

If that does not work either you can try eval(type+‘Insert’).call({}); (However eval is ,as far as I investigate, a little dangerous)

I’m not sure whether it is the best solution but it worked for me.


#3

Use ‘this’ instead of window. On client, ‘this’ is window. On server ‘this’ is global.