Hi, some days ago I encountered error about 'undefined is not a function' and some notes about anynomous function on the log. I've read some articles about anonymous function but still couldn't understand clearly. I just understand that it is a function without a name but have no idea about how it could be implemented on RMMV.
Anyone could help me explaining what is anonymous function and probably give example about how a beginner like me might create error on RMMV from that thing?
Thank you
It might be from an improperly-configured plugin, do you have any installed in your project?
Anonymous functions are primarily used to make sure variable you create outside of a function don't get put in the global scope, where any other plugin can see it. This is useful for getting stuff like parameters outside of a function. An anonymous function looks like this:
(function(){ // So stuff in here})();In order to use this to add functions to a class, you'd do something like this:
function myClass() {}(function(klass) { klass.variable = value klass.func = function(args) { // Create this function! }})(myClass);The last set of parenthesis tells JS to fun the function that we just defined in the first set of parenthesis.
In the first example, no class is put into the function, it's purely just an anonymous function. Anything defined in that will no longer be accessible after the function is done executing.
In the second example, we called the anonymous function with an argument, myClass. Inside the function, myClass is renamed to klass, because that's what we wanted to call it. A popular name to use in this case is simply '$'. Any of the definitions, like klass.variables, that begin with 'klass' will be added to myClass, because we are explicitly telling them to be added there. If I were to create a function without adding 'klass' before it, then that function would only be accessible to the stuff inside the anonymous function. This allows us to create functions that we can use in the external classes, but can't edit elsewhere. However, given the general community-centric way RM is structured, making functions where they can't be changed is not recommended, as this means anyone wanting to change part of your plugin in their own, or as a compatibility patch in the future, will have a much harder time since some of the code cannot be changed except directly in the original file.
I know it's a lot to understand, but hopefully this helped you out 🙂