Why we dont call the function direct

Hi

I am having a little trouble here why we dont call the function direct instead we are calling through another anonymous function.

const eventAssignment = (note) => { 
  note.onmousedown = function() {
    keyPlay(event);
  };
  note.onmouseup = function() {
    keyReturn(event);
  };
}

A little light on this matter please why the following is an issue

let eventAssignment = function(note){
  note.onmousedown = keyPlay;
  note.onmouseup = keyReturn;
  
}

kind regards

We will need a link to the exercise to be able to check your code.

One question I have just looking at the parameter… Is this function called on every key? Or is it a run once function that is supposed to register all the keys in one go?

Hi

Sorry for the missing information :frowning: Following is the topic
Piano Keys

My GitHub code

Kind Regards
Atif

1 Like

So it is a loop calling the function. Did you try the other approach?

Hi

It is working fine for me but the video for this project the instructor mentioned not to do this for some reason which is not explained point by point like what happens if we are not doing his way. (with example
Following is his way

let eventAssignment = function (note) {
   note.onmousedown = function () {
        keyPlay(event);
    }
    note.onmouseup = function () {
        keyReturn(event);
    } 
}

But what i did and looks working fine is the following

let eventAssignment = function (note) {
    note.onmousedown = keyPlay;
    note.onmouseup = keyReturn;
}

Following is the video link where he is explaining this point (like if we do this way it will redefine this function???) Did we cover this is in our course so far. How someone knows which way is the right way.
Step 4

Kind Regards
Atif

1 Like

His explanation is so vague, no wonder nobody can understand it. The callback reference does indeed work as expected. I’m not sure what it is that is being redefined. We need to do some deep reading into events and event handlers, which is a natural progression when learning something new like this. Don’t stop at just the lessons. Read up the documentation and any articles about do’s and don’t’s.