Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLeigh Maher
21,830 PointsRunning the roll function again and again
In this video Andrew moves the function out of the object because he says that it means the function, if left inside the object, has to run again and again.
I don't really get this. Doesn't this function have to run each time you pass a different number into i.e. the sides argument that gets passed in? This has to be passed into function, whether it lives inside our outside the object.
2 Answers
mrhummel
Courses Plus Student 1,765 PointsIf you have an anonymous function in the object, a new copy of the function will be created with every instance of the object. If you move it outside the function and reference it, it will only be there once. So if you would have 100 instances of an object, you would also have 100 copys of the anonymous functions inside the object which would eventually clutter up the memory.
Leigh Maher
21,830 PointsAh, ok. So, every instance of an object makes a copy of the code from that object? But we don't actually see that code, it's just contained within the instance that we create. And the function does run each time we create an instance of the object, but the function is just referenced?