arrow function used within an object literal
why does arrow function cause an issue - this.make and this.model are undefined but "normal" function(){} when used, theres no issue of undefined properties in the object literal
21 Replies
this
isn't bound to arrow functions
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions:
* Arrow functions don't have their own bindings to this, arguments, or super, and should not be used as methods.
doesnt it contain the lexical scope?
but in that cause its getting this from the window?
this
references Window
, yeah
but you shouldn't use arrow functions as methods
You can however define them like this:
okay thanks
didnt know arrow function has such drawbacks lol
thank you good sir!
as always, glad to help 🙂
testing it out with another code i have but this.width is getting an undefined error at the geometry property
I know it doesn't work, I just don't know why
you can use a getter instead, but you can't reference values of an object during its initialization like you are there.
this.color
would be undefined as well on the next line, and geometry
and material
on the one after
ah, it doesn't work because to reference the object you're in through this
it needs to be defined and initialized, and that's not done yetisnt width: 3
defined and has a value?>
width
is, floor
isn't, and this
would have to refer to floor
someone else might be better able to explain why exactly, but it won't work like you have it there
Here's the javascript.info article on getters and setters, you'll have to use a getter, which will have the added benefit of staying dynamic if you ever change width or height: https://javascript.info/property-accessorsokay thanks
if you do find out why it doesn't work, or if anyone else knows and wants to add it here, I'm definitely interested
sounds good
searching
chatgpt is confused, saying the variables arent within the floor object lol
The error message you're encountering, "width is undefined," is due to the fact that the width variable is not defined or accessible in the context of the object declaration.
In your code, the width and height variables are not defined within the floor object. To fix this issue, you need to either define width and height as separate variables before the floor object declaration or directly provide their values within the object.
the first sentence is correct though, it's not accessible in the context of the declaration
yes but wondering why
this is what chatgpt gave me:
const floor = {
width: 3,
height: 3,
textures: '',
color: 0xFF0,
// get createPlane(){
// new PlaneGeometry(this.width, this.heavy),
// new MeshBasicMaterial({ color: this.color })
// },
// Function to initialize object properties
initialize: function() {
this.geometry = new PlaneGeometry(this.width, this.height);
this.material = new MeshBasicMaterial({ color: this.color });
this.floor = new Mesh(this.geometry, this.material);
}
// geometry: new PlaneGeometry(this.width, this.height),
// material: new MeshBasicMaterial({ color: this.color }),
// floor: new Mesh( geometry, material ),
// createFloor: function()
// {
// console.log(this.width)
// }
}
floor.initialize();
works
makes no sense, same as mine
testing nowinitialize doesn't run until the last line where you call it with
floor.initialize()
, so it's definitely differentconst floor = {
width: 3,
height: 3,
textures: '',
color: 0xFF0,
createPlane(){
new PlaneGeometry(this.width, this.height),
new MeshBasicMaterial({ color: this.color }),
console.log(this.width)
},
}
did that originally, now it works lol
yeah, because you're not referencing
this
during declaration, just inside methods which you call afteryes makes now, thanks
because at the time of object initialization this refers to the window object
not the floor object
this works
floor gets placed into memory, then when floor.createPlane is called it gets the object and all its properties and etc
yes, exactly
perfect!
now, its better to turn this into a class right? incase I need to make copies of this
object literals arent a good approach to making copies?
probably yeah