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 trial

JavaScript jQuery Basics (2014) Creating a Simple Drawing Application Perform: Part 2

changeColor() or changeColor? I do not understand why the change event method receives changeColor and not changeColor()

When we are adding the change method to input why it receives as arguments changeColor not changeColor () with parentheses?.

I have understood that if you want to call a function, you must also place this changeColor parentis () and not changeColor.

Hi Vanessa,

I see what you mean, along about the 6 minute mark..

This is an example of passing a function "handle" or function "reference" to another routine.

Andrew is not really "calling" the function at that point.. If he was, you'd be right about him having to use the parens.

What he's doing is "injecting" the changeColor function into the jQuery routine. jQuery will remember this handle and use it when it needs a way to actually change the color. The changeColor routine is called elsewhere, not at the point Andrew mentions it.

Indirection like that can be hard to follow, even for experienced programmers. But it lets you say things like "Hey jQuery routine! If you want to change color, use this capacity to change color that I just gave to you!" Passing a reference to something is like passing a capability.

Take care, good luck! -- Cal

2 Answers

Vanessa,

This threw me off in the beginning as well. The reason you don't need the parens is because you want the function to be called upon a change. If you put the parens, it tells the javascript interpreter to call that functions immediately and then the result of that function (aka the return value) would be the actual parameter passed to the .change method.

So in other words you don't want to run the function until there is a change.

The jQuery function will handle the "calling" portion if that makes sense.

Jeremiah Lugtu
Jeremiah Lugtu
9,910 Points

Because

function changeColor(){...};

can be written as:

var changeColor = function(){...};

read more here: https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/