/ Published in: JavaScript

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Accepts a namespaced variable pointing to a class and returns a constructor * for that class. * * @see http://stackoverflow.com/questions/1366127/instantiate-a-javascript-object-using-a-string-to-define-the-class-name */ 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; };
Comments
