Created
March 21, 2017 08:23
-
-
Save yy-dev7/55b8752430a0ae4885e9b8a158555f5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function optionalChaining(obj, chain) { | |
return chain | |
.split('.') | |
.reduce(function(acc, val) { | |
return acc ? acc[val] : undefined; | |
}, obj); | |
} | |
var user = { address: { street: 'No.969 West WenYi Road', }, a: { b: { c: 2 } }, } | |
var ret = optionalChaining(user, 'address'); | |
console.log(ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment