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???
2 Answers
+ 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.
+ 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