I've got a little angular (v1.5.11) app where I'm trying to instantiate an object from a string variable. This code used to work in my old setup but after changing from bower/gulp to webpack (3.8.1), I'm now getting the error below. I'm not sure whether this is caused by my new import statement or something.
A similar question (Javascript "Not a Constructor" Exception while creating objects) dicussed the problem but I wasn't able to get get any of the solutions to work in my example which is why I'm thinking it might be down to the import or the pojo structure itself?
Error:
TypeError: $window[iteratorType] is not a constructor
Code:
MyService.js
import ListIterator from './util/listiterator.pojo.js';
MyService.$inject = ['$log', '$window'];
function MyService($log, $window) {
...
// this variable is obviously dynamic but for now I made it a static string
var iteratorType = 'ListIterator';
var iterator = new $window[iteratorType](); //<-- breaks as $window[iteratorType] is undefined
//var iterator = new ListIterator(); <-- this works just fine
...
listiterator.pojo.js
var ListIterator = function() {
this.executedIterations = 0;
this.iter = null;
};
...
export default ListIterator;