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.

[View All Snippets]
Show Plain Text »
  1. <script type="text/javascript">
  2. var className = "PluginClass";
  3. // get a reference to the class object itself
  4. // (we've assumed the class is defined in a global scope)
  5. var myclass = window[className];
  6. // now you have a reference to the object, the new keyword will work:
  7. var inst = new myclass();
  8. // now call to the required method of your class
  9. // alert(inst.validate("4111111111111111", "visa"));
  10. </script>
Create Your Free Account
Please remember that this information is essential to use our services correctly.
After creating the account you will be able to download all of our FREE products.
Fields marked with * are mandatory

Please send me information about updates, new products, specials and discounts from ApPHP!
We recommend that your password should be at least 6 characters long and should be different from your username/email. Please use only letters of the English alphabet to enter your name.

Your e-mail address must be valid. We use e-mail for communication purposes (order notifications, etc). Therefore, it is essential to provide a valid e-mail address to be able to use our services correctly.

All your private data is confidential. We will never sell, exchange or market it in any way. Please refer to Privacy Policy.

By clicking "Create Account", you are indicating that you have read and agree to the ApPHP Terms & Conditions.