1

The issue I have is that I am trying to construct an object in JavaScript, but I don't know what the object will be until runtime.

My code looks something like this:

var modelType = attrs.type;
return new modelType();

attrs.type will contain the name of a valid constructor. So for instance, it might be book, cat or dog. Depending on whatever it is, I want to construct that object. I cannot test what attrs.type is first and then choose the correct constructor because the types might change at any time in the future.

Currently the error I am getting is: Uncaught TypeError: string is not a function. (Since modelType is a string...)

Any help would be greatly appreciated!

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.