Skip to content

Instantly share code, notes, and snippets.

@aozora
Last active January 10, 2018 11:15
Show Gist options
  • Save aozora/12341bc519e5eb9a55ca to your computer and use it in GitHub Desktop.
Save aozora/12341bc519e5eb9a55ca to your computer and use it in GitHub Desktop.
// In this function we’re incrementing the count
// property on the object that was passed in
// but there's a bug..
function incrementCount(counter) {
if (counter.count) {
counter.count++;
} else {
var counter = 1;
counter.count = counter;
}
}
// What happens when we call it in these 3 ways? comment each one.
incrementCount(0);
incrementCount({
count: 10
});
incrementCount();
@aozora
Copy link
Author

aozora commented Jan 10, 2018

The 3rd give this error: Uncaught TypeError: Cannot read property 'count' of undefined
The first 2 do nothing, since there's no "return" in the function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment