Assessment wanted for Object basics 2 skill test!

Hello guys! I hope you’re okay! Me, I’m struggling with this test!
Actually, I solved it but I wanted to try another “version” of this test and I failed. Would someone explain to me why?
This is the correct version: https://codepen.io/GabriPast_04/pen/zYwyMyE
and this is the one I failed: https://codepen.io/GabriPast_04/pen/zYwyMXP

Have a good day! :wink:

Hi @Gabriele :wave:

The problem is the context of this. When used inside the myBand object, one might expect the myBand object to be the context. That is incorrect. It’s the global object. Such this references work inside a constructor function but not inside an object.

To make your example work, you could turn info into an function getInfo which returns a string:

getInfo : function() {
  return this.name + " was born in " + this.formed + ".";
}

and then call the function to put the value into bandInfo:

bandInfo = myBand.getInfo();

Because we are now using it inside a function, this gets the correct context (our myBand object).

Hopefully, my explanation was clear. Otherwise just ask more questions. I’m happy to answer them :slightly_smiling_face:

Michael

1 Like

If you want to read more about this in JavaScript, I recommend: MDN | this
Another recent post about the trickiness of this: Clarifying question about OOJS 1: arrow functions to define new method outside constructor

1 Like

Hey, I’m so sorry to reply this late. I actually understood what’s wrong, I didn’t know that, thanks so much for the explanation!
Have a good day :wink:

1 Like

Absolutely no problem :slight_smile:
I’m happy it was helpful.

All the best!
Michael