Hello I found an article on stackoverflow how to call dynamic function:
function mainfunc(func) {
this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}
This is very useful. But how can I call a function in my objects functions?
I get an error:
“TypeError: this[func] is undefined
this[func].apply(this, Array.prototype.slice.call(arguments, 1));”
it looks like:
var myScript = {
...
add : function() {
...
myScript.ajax('url.php', params, 'myScript.add2List');
},
add2List : function(data) {
console.log(data);
},
ajax : function(url, params, func) {
$.ajax({
type: "POST",
url: url,
data: params,
success: function(data) {
console.log(func);
mainfunc(func, data);
}
});
}
}
You don’t need to use that function, you could do the below instead.