What does this refer to in a JavaScript function? -
function box(width, height) { this.width = width; this.height = height; } var mybox = new box(5,5);
what
new
keyword doing here technically? creating new function? or creating new object , applying function it?if way create "box", mean
this
keyword referring object mybox?
it's creating new object, using box
constructor. value of this
in case (when function called new
keyword) new instance being constructed. new object inherit whatever defined box.prototype
(the default being object.prototype
).
i said in case, because in javascript value of this
determined how function called. recommend reading mdn page on this
more information.
note: if question supposed closed, should have been duplicate. here possible duplicate links might you:
Comments
Post a Comment