Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 0.38 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get object class from string name in javascript
  2. var classes = {
  3.    A: <object here>,
  4.    B: <object here>,
  5.    ...
  6. };
  7.        
  8. new classes[name]()
  9.        
  10. function foo() { }
  11.  this["foo"]();
  12.        
  13. function Person(name){
  14.     this.name = name;
  15. }
  16.  
  17. function getClass(str_name, args){
  18.     return new (window[str_name])(args);
  19. }
  20.  
  21. var wong2 = getClass("Person", "wong2");
  22.  
  23. alert(wong2.name);   // wong2