Skip to content

Instantly share code, notes, and snippets.

@asiniy
Created July 4, 2018 07:44
Show Gist options
  • Save asiniy/f22488a0301fff7422f9ba4f8c368a37 to your computer and use it in GitHub Desktop.
Save asiniy/f22488a0301fff7422f9ba4f8c368a37 to your computer and use it in GitHub Desktop.
objectFetch // Extending JavaScript with ease (beginner level tutorial)
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