Skip to content

Instantly share code, notes, and snippets.

@hankyates
Last active August 29, 2015 13:57
Show Gist options
  • Save hankyates/9680619 to your computer and use it in GitHub Desktop.
Save hankyates/9680619 to your computer and use it in GitHub Desktop.
// Implement a decorator function that takes
// a function as an argument and will track
// how many times the passed function was called.
function Add(x, y) {
return x + y;
}
var Add = countDecorator(Add);
Add(1, 1);
// -> 2 Dont pay attention to this number. The important part is we called the `Add` function once.
Add.callCount();
// -> 1
Add(2, 2);
// -> 4 Dont pay attention to this number either. The important part is we called the `Add` function again.
Add.callCount();
// -> 2
@kgentner
Copy link

@hankyates - Hi Hank, Would you please add the expected outcome if "addCallCount();" was called a few more times? Example:

addCallCount(); //expected outcome
Add(1,1);
addCallCount(); //expected outcome
addCallCount(); //expected outcome
Add(2,2);
addCallCount(); //expected outcome

@CooperAtive
Copy link

hey kgenter, I believe that simply calling addCallCount should return the current number of times Add has been called, so:
addCallCount(); //expected outcome = 0
Add(1,1);
addCallCount(); //expected outcome = 1
addCallCount(); //expected outcome = 1
Add(2,2);
addCallCount(); //expected outcome = 2

@ChelseaLura
Copy link

codefellows/sea-c11-javascript#67 (comment)

I was reading through others' code for ideas about this assignment and I noticed this comment you(Hank Yates) made. I am really confused because it seems like you say we need to replace the add function. However in the example above it looks like we are only passing the Add function to our decorator function; not reassigning add. I'm struggling to find a way to answer this question without reassigning Add.

@hankyates
Copy link
Author

Whoops. yeah yall are right. does the updated gist make it a little more clear?

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