DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Chara has posted 50 posts at DZone. View Full User Profile

Create new Object From Variable in JavaScript

06.30.2014
| 3212 views |
  • submit to reddit
<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>

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.