I got a string that I want to turn into a function. I've tried eval and that works just fine, but I've heard eval is dangerous.
function setProductList(list) {
for(typeList of list) {
let modelName = `model${typeList[0].type}Products`
typeList = JSON.stringify(typeList)
let model = Function("return " + modelName)()
model.setData(typeList)
}
}
So, I want to turn modelName
into a function. If I do modelName = Eval(modelName)
it works perfectly and I can use modelName.setData().
But with this method I am trying right now, it says modelXXProducts
is not defined. How can I turn the string into a function without eval?