Spr243 0 Posted November 8, 2020 Report Share Posted November 8, 2020 (edited) How can I run a method stored in a variable? For example, there is a dropdown of available methods and the selects a method that the user wants it to complete. That method is stored in the variable, userSelectedMethod. For the example, that method would be toUpperCase(). var userSelectedMethod = "toUpperCase()" I want the script to run the method that is in the variable and apply it to the enteredText variable. I tried using the following code. (Didn't work, obviously) /// Attempt 1 var newResult = enteredText.userSelectedMethod; /// Attempt 2 var newResult = enteredText.Module[userSelectedMethod]; /// Attempt 3 var newResult = enteredText.callback(userSelectedMethod); I want the newResult to run whatever method the user's selected method is. What would be the proper and simplest method of accomplishing this? Note: The variable names and extra steps that I had in my project, from which this question arose, have been removed and simplified to explain what I'm trying to do. Edited November 8, 2020 by Spr243 Clarification Quote Link to post Share on other sites
Ingolme 1,027 Posted November 9, 2020 Report Share Posted November 9, 2020 The safest way is to decide a list of permitted methods, and then use a switch() statement to choose which one you want to run: var newResult; switch(userSelectedMethod) { case "toUpperCase()": newResult = enteredText.toUpperCase(); break; case "toLowerCase()": newResult = enteredText.toLowerCase(); break; default: newResult = enteredText; } If you tried to generalize further than that, it's likely that the user will break something by putting invalid methods or something that could alter the control of your program. Quote Link to post Share on other sites
Spr243 0 Posted November 9, 2020 Author Report Share Posted November 9, 2020 39 minutes ago, Ingolme said: The safest way is to decide a list of permitted methods, and then use a switch() statement to choose which one you want to run: var newResult; switch(userSelectedMethod) { case "toUpperCase()": newResult = enteredText.toUpperCase(); break; case "toLowerCase()": newResult = enteredText.toLowerCase(); break; default: newResult = enteredText; } If you tried to generalize further than that, it's likely that the user will break something by putting invalid methods or something that could alter the control of your program. Thank you for your response. My actual intended use is for programming a bot in Discord, but I figured the general principle would be the same. I will be aware of the potential security implications, but this bot is private and limited in its use to trusted people. Ignoring security issues, what would be the way to do what I mentioned in my original post, as making switch statements for each would take tremendous amounts of effort for the goal I am trying to achieve. Quote Link to post Share on other sites
Ingolme 1,027 Posted November 9, 2020 Report Share Posted November 9, 2020 The more general solution would be to have a string containing the name of the method and use the square brackets notation to access it. var methodName = "toUpperCase"; var str = "Hello World!"; var result = str[methodName](); This solution only works with methods that don't take arguments, of course. 1 Quote Link to post Share on other sites
Spr243 0 Posted November 9, 2020 Author Report Share Posted November 9, 2020 13 hours ago, Ingolme said: The more general solution would be to have a string containing the name of the method and use the square brackets notation to access it. var methodName = "toUpperCase"; var str = "Hello World!"; var result = str[methodName](); This solution only works with methods that don't take arguments, of course. Thank you again for your response. I really appreciate it. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.