Skip to content

Instantly share code, notes, and snippets.

@TyIsI
Last active May 5, 2016 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TyIsI/d5a59b555178d6c0ef3f7ee2ba701a8e to your computer and use it in GitHub Desktop.
Save TyIsI/d5a59b555178d6c0ef3f7ee2ba701a8e to your computer and use it in GitHub Desktop.
Javascript dynamic class loading and dynamic method loading
// Example class
function ExampleClass() {
// Example Method
this.ExampleMethod = function ( args ) {
// Log to console
return console.log( "Name: " + args.message );
}
}
// Variables
var className = 'ExampleClass';
var methodName = 'ExampleMethod';
var args = { "message" : "Success!" };
// Set up objectsArray (cache)
var objectsArray = {};
// Dynamically create new instance
objectsArray[className] = new window[className]();
// Dynamically call the method
objectsArray[className][methodName]( args );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment