Why don't inline functions work but arrow functions do?



When I create an event handler like this:

$(".foo").on("click", function() {
  //insert code here

the website works as the instructions say it should, but the question is marked wrong.

Can you tell me why only “(fat) arrow functions” () => {}; work and not inline functions function() {}; work? They both work in jQuery normally as they should in the tutorial.



What lesson are you on? Please post the URL from the location bar of the exercise page.


Well, it applies to nearly every secttion, but the one I’m on is: https://www.codecademy.com/courses/learn-jquery-event-handlers/lessons/mouse-events/exercises/intro-to-mouse-events?action=resume_content_item&course_redirect=learn-jquery


Are you getting any error message from the lesson checker?

Something of note concerning arrow functions. They do not have a context object (no this) so the function cannot be written with a context.

sayHello () {
    console.log(`Hello, ${this.name}`);

will raise an exception. this.name is not defined.

What code pattern is expected? What code are you entering for this exercise?


The this keyword is not related to any of the exercises I’m doing.

This is the code I used for the tutorial:

  $('.login-button').on('click', function() {

I would expect the function() {} keywords to work the same way as () => {} functions. Anyway, it works in the JavaScript tutorial.

And I am getting the following error message:

Did you change ’ __ ’ to ‘click’?

when I have done that.

Error messages basically appear when I use function() keyword.


It will in time. I could see it wasn’t in this instance, though. Just tossed in that point.

The error message may be the only one in store for this SCT. If it appears only when you use function () instead of () => then that means it will only accept the latter.


No worries, thanks for the advice.

Can anyone try and fix it?


What solution are you submitting?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.