Hi I am creating a simple object in js by which it has id, and slug. The grace is that through this I can access the id of this, when I request the output of the object it gives me an undefined value.
const post = {
id: 1,
slug: `post-${this.id}`
}
console.log(post);
You need a function to change the context and with it the value of
this
so you can use theid
:An anonymous function can give erroneous results by not changing the context, you would not be using the object's value.
You can put it inside an anonymous method and in this way encapsulate the value and the result, the detail is that you would access it as a method