importance of not writing parenthesis ‘( )’ after function name in callbacks

Suppose you want to assign an event handler function to an click event of any element

$(document).on('click', '.disable-user', disableUserConfirmation());

when you run the app the function disableUserConfirmation()  gets executed any way, however the event handler function expected to run when the event is triggered wont work. You can see the error in the console.

Uncaught TypeError: undefined is not a function

What happens in callbacks is, when the jQuery’s function ‘on’ want to execute the function object it just appends the parenthesis after the function name and executes it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s