Skip to content

Instantly share code, notes, and snippets.

@apphp
Created July 1, 2014 08:19
Show Gist options
  • Select an option

  • Save apphp/ae2f5bd22d532dbaf695 to your computer and use it in GitHub Desktop.

Select an option

Save apphp/ae2f5bd22d532dbaf695 to your computer and use it in GitHub Desktop.
Create new Object From Variable in JavaScript
/*
This example of code allows you to create a new object in javascript (using simple inheritance)
such that the class of the object is defined from a variable. After creating the object you may use
it for your purposes.
source: http://www.apphp.com/index.php?snippet=javascript-create-object-from-variable
*/
<script type="text/javascript">
var className = "PluginClass";
// get a reference to the class object itself
// (we've assumed the class is defined in a global scope)
var myclass = window[className];
// now you have a reference to the object, the new keyword will work:
var inst = new myclass();
// now call to the required method of your class
// alert(inst.validate("4111111111111111", "visa"));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment