to avoid endless checking typeof(o) !== 'undefined'
and o !== null
I wrote the following function:
function tc(o, expr) {
try {
return eval('o.' + expr);
}
catch(e) {
return false;
}
}
which can be used like this:
var obj = {
person: {
character: [
'smart',
'slow'
]
}
}
if (tc(obj, 'person.character.length > 1')) {
console.log('Give him a chance');
}
instead of writing:
if ((typeof(obj) !== 'undefined') && (obj !== null) &&
(typeof(obj.person) !== 'undefined') && (obj.person !== null) &&
(typeof(obj.person.character) !== 'undefined') && (obj.person.character !== null) &&
(obj.person.character.length > 1)) {
console.log('Give him a chance');
}
and I wanted to know if this solution have any performance or security (because of the eval statement) issues.