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


#1

Hi,

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.

alphabeta367


#2

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


#3

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


#4

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?


#5

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() {
    $('.login-form').show();
  })
  
  
});

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.


#6

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.


#7

No worries, thanks for the advice.

Can anyone try and fix it?


#8

What solution are you submitting?


#9

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