You can create a general function for this by passing it a string representing the path to the attached property:
function getValue(object, prop, valIfUndefined) {
var propsArray = prop.split(".");
while(propsArray.length > 0) {
var currentProp = propsArray.shift();
if (object.hasOwnProperty(currentProp)) {
object = object[currentProp];
} else {
if (valIfUndefined) {
return valIfUndefined;
} else {
return undefined;
}
}
}
return object;
}
Then use this on any object, for example:
if (getValue(ctrl, 'main.user', null)) {
}