Creating a JavaScript object using a string to determine the class name

Here is what I am trying to do - it is pseudo code and does not work. Does anyone know how to do this for real:

// Define the class MyClass = Class.extend({}); // Store the class name in a string var classNameString = 'MyClass'; // Instantiate the object using the class name string var myObject = new classNameString(); 
+51
javascript oop
Sep 02 '09 at 6:34
source share
6 answers

Would this work if you did something like this:

 var myObject = window[classNameString]; 

..

+47
02 Sep '09 at 6:37
source share

Here's a more robust solution that will work with functions superimposed on names:

 var stringToFunction = function(str) { var arr = str.split("."); var fn = (window || this); for (var i = 0, len = arr.length; i < len; i++) { fn = fn[arr[i]]; } if (typeof fn !== "function") { throw new Error("function not found"); } return fn; }; 

Example:

 my = {}; my.namespaced = {}; (my.namespaced.MyClass = function() { console.log("constructed"); }).prototype = { do: function() { console.log("doing"); } }; var MyClass = stringToFunction("my.namespaced.MyClass"); var instance = new MyClass(); instance.do(); 
+52
Mar 14 '10 at 11:25
source share

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; } // and then var obj = new global[classNameString] 

From: How to get global object in JavaScript?

+20
Sep 02 '09 at 7:07
source share

If MyClass is global, you can access it as a property of a window object (assuming your code is running in a browser) using substring notation.

 var myObject = new window["MyClass"](); 
+11
Sep 02 '09 at 6:42
source share

Here is an improved version of the Yuriy method, which also processes objects.

 var stringToObject = function(str, type) { type = type || "object"; // can pass "function" var arr = str.split("."); var fn = (window || this); for (var i = 0, len = arr.length; i < len; i++) { fn = fn[arr[i]]; } if (typeof fn !== type) { throw new Error(type +" not found: " + str); } return fn; }; 
+3
Nov 13 '12 at 11:06
source share

If classNameString comes from a secure source, you can use

 var classNameString = 'MyClass'; var myObject = eval("new " + classNameString + "()"); 

This solution works with namespaces and is platform independent (browser / server).

0
Jul 09 '16 at 10:01
source share



All Articles