BTW: window - a reference to a global object in the JavaScript browser. This is also this
and should work even in non-browser environments such as Node.js, Chrome extensions, translatable code, etc.
var obj = new this[classNameString]();
The limitation is that the called class must be in a global context. If you want to apply the same to a scope class, you need to do:
var obj = (Function('return new ' + classNameString))()
However, there really is no reason to use a string. JavaScript functions themselves are objects, just like strings, which are also objects.
Edit
Here is the best way to get a global scope that works in strict mode as well as in non-browser JS environments:
var global; try { global = Function('return this')() || (42, eval)('this'); } catch(e) { global = window; }
From: How to get global object in JavaScript?
bucabay Sep 02 '09 at 7:07 2009-09-02 07:07
source share