2
function Box(width, height)
{
  this.width = width;
  this.height = height;
}

var myBox = new Box(5,5);
  1. What is the new keyword doing here technically? Is it creating a new function? Or is it creating a new object and applying the function to it?

  2. If so then this is a way to create a "Box", does this mean the this keyword is actually referring to the object myBox?

Lews Therin
  • 10,709
  • 4
  • 43
  • 69
edaniels
  • 628
  • 4
  • 23
  • See here: http://www.quirksmode.org/js/this.html – Jonathan M May 28 '13 at 16:23
  • 1
    basic principles of object oriented programming. better find a good starters guide to oop – Sharky May 28 '13 at 16:24
  • I'm voting to close this as not a real question because it's a little too broad IMO. – zzzzBov May 28 '13 at 16:24
  • 1
    @Sharky meh... This is JavaScript, "normal" OOP doesn't apply here imo. Valid question methinks. – Lews Therin May 28 '13 at 16:25
  • @zzzzBov I read the question as being specific to that example code. But I'm still looking for a duplicate. – bfavaretto May 28 '13 at 16:25
  • @LewsTherin i dont think OP has reached the level of being able to discuss if javascript is truly OOP or not. he doesnt know what "this" represents. – Sharky May 28 '13 at 16:28
  • 2
    http://stackoverflow.com/questions/133973/how-does-this-keyword-work-within-a-javascript-object-literal –  May 28 '13 at 16:28
  • Voted to reopen it. I think the question just needed a bit of order. – Lews Therin May 28 '13 at 16:29
  • @Sharky The OP clearly has some idea of what `this` should do. What he needs is clarification: "applying `new` to a function call creates an object in JavaScript.. blah blah". Shrugs. – Lews Therin May 28 '13 at 16:31
  • @LewsTherin, I still disagree. It covers a few too many topics, and has been answered by a number of other existing questions. I don't feel that having new answers to this one is necessary. – zzzzBov May 28 '13 at 16:31
  • 1
    I don't think this question is worthy of an `open/close` war. This is basic information that has been explained many times on SO. –  May 28 '13 at 16:32
  • @zzzzBov Sure, but at least "close as duplicate" would be fine. Lead the OP to some answers.. no? – Lews Therin May 28 '13 at 16:33
  • Not really NARQy at all. (voted to reopen) – tckmn May 28 '13 at 20:45

1 Answers1

9

It's creating a new object, using Box as its constructor. The value of this in this case (when the function is called with the new keyword) is the new instance being constructed. This new object will inherit from whatever is defined as Box.prototype (the default being Object.prototype).

I said in this case, because in JavaScript the value of this is determined by how the function is called. I recommend reading the MDN page on this for more information.


Note: if this question is supposed to be closed, it should have been as a duplicate. Here are some possible duplicate links that might also help you:

Community
  • 1
  • 1
bfavaretto
  • 69,385
  • 15
  • 102
  • 145