Objects 10/15


#1

No idea why this was throwing an error and making my name undefined.


Any eagle eyes to spot an error?


#2

Here’s the error 06


#3

arrow functions’ this is that of the surrounding scope, use a function expression instead

function() {}

first paragraph of
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

section Arrow Functions of
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

And the way you could have reasoned about this is that you can tell from the error message that this is undefined, and then you could start looking at references for how this is set (like the above links)


#4

thanks ionatan! i was having the same issue. So we can’t use arrow functions with ‘this’?
Will ‘this’ work with the ES6 method creation?

ie would this work for:

sayGoodbye() {
return 'Goodbye!;
}

ie:

sayGoodbye () {
return" Goodbye, ${this.name} ";

\the “” are because it appears that ` creates a quote block.


#5

Behaving differently isn’t the same as unusable


#6

ah, sorry i just read your links until now. i was confused because i read that ES6 was best practice for method creation, i’ll pay careful attention with using this in the future. thanks for the reply!


#7

Maybe they meant something else. MDN, which is a pretty reliable source, says the opposite of that – because methods involve binding the function to the object, and that’s exactly what arrow functions do not do


#8

oh o.k. good to know. I’ll make note of that. thanks! Everything is still very new to me. I’m learning a lot though!


#9

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