Created
July 4, 2018 07:44
-
-
Save asiniy/f22488a0301fff7422f9ba4f8c368a37 to your computer and use it in GitHub Desktop.
objectFetch // Extending JavaScript with ease (beginner level tutorial)
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 objectFetch = (object, propertyName) => { | |
if (object.hasOwnProperty(propertyName)) { | |
return object[propertyName] | |
} | |
throw new ReferenceError(`key not found: ${propertyName}`) | |
} | |
const collectorGroup = { target_count: 10 } | |
objectFetch(collectorGroup, 'target_count') // returns 10 | |
objectFetch(collectorGroup, 'targets_count') // throws ReferenceError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment