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); 
  1. what new keyword doing here technically? creating new function? or creating new object , applying function it?

  2. 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

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -