So every time I access a property of an object which is not under my authority, do I have to check existence and type?
Yes you will have to check the whole path, once at a time, or you can automate it:
function deepObject(o, s) {
var ss = s.split(".");
while( o && ss.length ) {
o = o[ss.shift()];
}
return o;
}
var isOk = deepObject(e, "originalEvent.originalEvent.touches.0.webkitForce");
if ( isOk ) {
// isOk is e.originalEvent.originalEvent.touches.0.webkitForce;
}
Test case:
var o = {
a: {
b: {
c: {
d: {
e: {
}
}
}
}
}
}
var a = deepObject(o, "a.b.c");
var b = deepObject(a, "d");
console.log(a); // {"d": {"e": {}}}
console.log(b); // {"e": {}}
console.log(deepObject(o, "1.2.3.3")); // undefined