0

Question about this keyword(shouldnt be too complicated).

If I make a obj constructor like: var name = function(w, h, x, y) { /*MY QUESTION IS HERE: I must use [this.] to assign a parameter shorthand to assign it to a declared var higher on the Dom scope, right. Well I can't seem to figure out if I write it like this: */ this.w = width; //Or this width = w; // It doesn't seem to work very well unless this.word = word not this.wrd = word. //So what assignment (L or R) references the parameter (var) name you put into and which references the value from outside the function like w = c.width???

6th Jun 2022, 2:05 AM
pHANTOM|sTEELE
2 Respostas
+ 1
The „this“ keyword is used to access or reference the parameter of the object for example in your consturctor you use: this.width = w; to assign the value of the function parameter w to the width attribute from the object that is created using the constructor. So this is used to access the attribute of the object that calls the function. The this-keyword makes it possible to use the same name for the attribute of the object and for the parameter without confusing them. I hope this answers your question.
6th Jun 2022, 8:52 AM
TheCoder25
TheCoder25 - avatar
+ 1
Ok, so if your fn has parameters like {w, h} the "this." refers actually to the outside variable not the other way around. I've been trying this.w = width. Cool thx
6th Jun 2022, 1:56 PM
pHANTOM|sTEELE