I didnt try to solve the challenge. It was more a general question, how can i take the example from the code snippet and turn it into an arrow function. Withouth changing the functionality of the code. So that " console.log(person.sayHello());" will give the output:
You couldn’t have done it how you were trying to do it originally because you can’t declare variables in an object, that doesn’t make sense, makes no difference if you use an arrow function or the function keyword.
Its not a stupid task, i know that moment when you are facing a challenge, but in the midst of it you discover another notion you want to tackle and thats the best way to learn new content, by observing its behavior in authentic environment.
As you have already figured out, by other responses, it wouldnt be that hard to write an object method in the form of an arrow function, but it is not recommended, because of how this behaves for arrow functions. In regular functions, this refers to the owner of the function; they “bind” their this to their owner. In the challenge case, the owner of the function is the object(which makes the function its method). Arrow functions do not have their own this(which is bound to refer to their owner), instead they inherit the this of their parent scope. In the challenge case, if we were to write an arrow function as the method of the example object, the parent scope is the “window”.
Okay, i understand it is not recommended to change the recommended code into an arrow function. But if its so “easy” and “trivial” can someone do it anyway?
Just to see the hypothetical solution.
I tried it now with the pattern of DanCouper and it still doesnt work.
const person = {
name: "Taylor",
sayHello: () => `Hello! My name is ${this.name}.`;
};
Sorry i am a bit “frustrated” because you all say: its so easy change the code into an arrowfunction if you want, despite its not recommenmded. And then i try it and it just give me an error
That there, that’s an arrow function, there’s nothing more to it than that, so what isn’t working here? What does the error say? It will tell you what the issue is.
You have an object. What is the syntax for an object? Here’s an example:
{
name: "Dan",
occupation: "programmer",
}
What is wrong with the code you’ve written? What is there in your object that doesn’t belong in an object? It’s got nothing to do with the property being a function: properties can be any value, but the syntax you’ve used is wrong
well you did just what you wanted, you turned the object method into an arrow function, but like i pointed out, the this behaves differently for arrow functions compared to regular functions and since the challenge relies on this, you cant meet the challenge requirement using an arrow function. The challenge itself is based on writing a shorthand method of a regular function
Like the original codesnippet. But it seems to be impossible with arrow function. Instead i always get an error. So am a bit confused, that you all say its easy to turn the original codesnippet into an arrowfunction.
Or do you mean: Yeah you can turn it easily into an arrowfunction, but then it will give you an error?
Open curly bracket
Name of property
Colon
Value of property
Comma
Name of property
Colon
Value of property
Comma
…etc
Close curly bracket
It is literally highlighting the error. You are defining an object. You have to use object syntax.
How do you separate properties in an object? you do not use semicolons
You will then get the issue everyone else is talking about. But in general there is absolutely no problem using arrow functions as the values in an object, they are just a value that you can define.
It is just that in this example, it won’t work, and you need to be aware that this [deliberately] works slightly differently in arrow functions. (Edit: saying it doesn’t work would be incorrect, it will work exactly as designed, it just won’t do what you want it to do)
As @kevinSmith points out, using arrow functions as object methods is not encouraged, but did you follow the reason? Arrow functions have no this. So when you refer to this.name, the arrow doesn’t have one, so it refers to the object - and the object’s this (the context in which the object exists) is not what you think.
There are ways to use them in a returned object, just not in that way. When you learn about modules or factory functions, a returned object with arrow functions accessing that scope works. But until you get this and what context means, using arrow functions in this way is going to be problematic.
i overlooked this line of code. As Dan wrote , you should pay more attention on the syntax within the objectbody. Initially you tried to use let to define object properties, which is wrong and now you finish a line within object with semicolons. Maybe you are confusing the arrow function and regular function syntax.
Here the semicolon marks the end of the return line and is within the function body.
Here the semicolon is outside the arrow function body, thus inside the object body
But you’re making very basic syntax errors here (trying to put a variable into it, or using semicolons to seperate properties). There’s nothing special about the value of an object property being an arrow function in terms of whether it’s possible: it’s just another value
You can define
{
example: function () {
doSomething
}
}
As
{
example() {
doSomething
}
}
But that’s just shorthand, added to the language syntax because it’s annoying to have to write example: function() { over and over again.
Thank you guys, i think i have to look through this thread while the next week. To be honest i understand very vaguely, that i cant use this with an arrowfunction.
The main-thing was, that i cant transform the codesnippet into an arrow function. Or i could do it, but because of the this-keyword in this specific codesnippet will give me an error after the “transformation”.
Again thanks for the big effort and sorry for my “stupidity”
In the context of the particular lesson in question, arrow functions won’t work, no. Syntactically you can write them, and that’s what @DanCouper was showing you: that property names in an object can contain traditional functions, arrow functions, strings, other objects…whatever you like.
const person = {
name: "Snowmonkey",
// moodRing works, because no this keyword
moodRing: ()=>{
const emotions=["happy","sad","angry","silly","apathetic"];
return emotions[ Math.floor( Math.random() * emotions.length ) ]
}
}
// letsuse them in a sentence!
console.log( person.name + " feels "+person.moodRing() )
As i said, later you’ll find some great places to use arrow functions in objects, but the MDN disclaimer is in place because, if you don’t understand this, things can get wobbly.
Why is the output showing me an undefined after i invoked a function of the object? Whereas if i console.log for the “simple” keys like arr or num it its only showing the value. Or it is a very complicated reason not to understand for a beginner?