I hereby claim:
- I am pichardoj on github.
- I am jpichardo (https://keybase.io/jpichardo) on keybase.
- I have a public key ASCQ75Bt6nqP1GB6T-CVvSsWco0g48HHICv3uA9j0B5m-go
To claim this, I am signing this object:
I hereby claim:
To claim this, I am signing this object:
function optionalAccess(obj, path, def) { | |
const propNames = path.replace(/\]|\)/, "").split(/\.|\[|\(/); | |
return propNames.reduce((acc, prop) => acc[prop] || def, obj); | |
} | |
function proxyOptional(obj, evalFunc, def) { | |
const handler = { | |
get: function(target, prop, receiver) { | |
const res = Reflect.get(...arguments); |
const evaluation = obj && obj.prop1 && obj.prop1.prop2 && obj.prop1.prop2.prop3; | |
console.log( evaluation != null ? evaluation : "SomeDefaultValue" ); |
SomeClass object; | |
Optional.ofNullable(object) | |
.map(obj -> obj.prop1) | |
.map(obj -> obj.prop2) | |
.map(obj -> obj.prop3) | |
.orElse("SomeDefaultValue"); |
val object: SomeClass? | |
object?.prop1?.prop2?.prop3 ?: "SomeDefaultValue"; |
const evaluation = obj && obj.prop1 && obj.prop1.prop2 && obj.prop1.prop2.prop3; | |
console.log( evaluation != null ? evaluation : "SomeDefaultValue" ); // => "SomeDefaultValue" |
SomeClass obj; | |
obj?.prop1?.prop2?.prop3 ?? "SomeDefaultValue"; |
let obj; | |
console.log(obj.someProp); // => TypeError: Cannot read property 'someProp' of undefined | |
obj = null; | |
console.log(obj.someProp); // => TypeError: Cannot read property 'someProp' of null |
let obj; | |
console.log(obj && obj.someProp); // Prints undefined |
console.log( obj && obj.prop1 && obj.prop1.prop2 && obj.prop1.prop2.prop3 ); // => undefined |