Created
May 3, 2016 21:30
-
-
Save danillouz/600b569b543ed0d13feda17ecbe9ef1c to your computer and use it in GitHub Desktop.
Check yourself before you destructure yourself.
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
const data = { | |
entry: [ | |
{ | |
messaging: [ | |
{ | |
senderId: 1, | |
payload: { | |
text: 'hello' | |
} | |
} | |
] | |
} | |
] | |
}; | |
Promise | |
.resolve() | |
.then(() => { | |
for (const entry of data.entry) { | |
for (const { | |
// We extract `payload` by destructuring an element from the `entry.messaging` Array, | |
// but because it's an Object which in turn is destructured, `:` must be used. | |
// Because `:` in a destructuring statement assigns to a variable with a different name | |
// than the (destructured) object property, i.e. `payload`, that variable is NOT defined. | |
payload: { text } | |
} of entry.messaging) { | |
if (payload) { // throws an error because payload is not defined | |
console.log(`message text: ${text}`); | |
} | |
} | |
} | |
}) | |
.catch(err => console.log(`${err}`)); // ReferenceError: payload is not defined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment