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 trialJustin Sze Wei Teo
9,418 PointschangeColor() comes before its function changeColor() is defined. This is ok?
Hi all,
As per the title, I notice that in this video tutorial, changeColor() is run before function changeColor(){....} is defined.
Why is this possible?
See code extracted from tutorial below,
//When clicking on New Color button $("#revealColorSelect").click(function(){ //Show colorSelect div or hide colorSelect div changeColor(); $("#colorSelect").toggle();
});
//update the new color span function changeColor() { var r = $("#red").val(); var g = $("#green").val(); var b = $("#blue").val();
1 Answer
Steven Parker
231,269 PointsThis is an example of "hoisting".
This is a principle of many modern languages where declarations are "virtually raised" to the top of their scope. This allows forward references to function correctly and not generate errors.
Justin Sze Wei Teo
9,418 PointsJustin Sze Wei Teo
9,418 PointsThat helps thanks Steven Parker. Alternatively, is it always safer to declare the function before using it to run?