Is there a way to do the following without using eval()?
The following function accepts an array of strings where the strings are object names. I iterate over them to ensure that none of them are undefined. I'd like to be able to do this without using eval()
function hasMissingDependencies(dependencies) {
var missingDependencies = [];
for (var i = 0; i < dependencies.length; i++) {
if (eval('typeof ' + dependencies[i]) === 'undefined') {
missingDependencies.push(dependencies[i]);
}
}
if (missingDependencies.length > 0) {
return true;
}
return false;
}
With this, I'm able to pass something like the following and get what I want:
alert(hasMissingDependencies(['App.FooObject', 'App.BarObject']));
I'd prefer to not use eval() but haven't found another way to check if an object is undefined when the object names are passed as strings.
hasMissingDependencies(App.FooObject, App.BarObject)throws an error before ever making it into the function if either of those objects are not defined. Am I missing something?Appisundefinedornull, then yes, it would throw an error because you're trying to dereference a null reference. But ifAppis notundefinedornull, then you can pass those two arguments and then just check if they areundefinedin the function, which would be checking if theFooObjectandBarObjectproperties exist on theAppobject.