0
function myFunc(theObject) {  
      theObject = new TheObject("Ford","Focus",2006);  
} 

Why is new TheObject() used instead of something like new Object()? I don't understand.

DarkLightA
  • 14,980
  • 18
  • 49
  • 57

4 Answers4

4

There's a function TheObject(...) "class" somewhere this is creating that occurs before this in your included code, that's what it's creating.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
3

TheObject is a user defined object.

Samet Atdag
  • 982
  • 6
  • 21
3

For the code you posted to work, somewhere else on the same page has to be something like the following:

var TheObject = function(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

Then, your posted code will create a new object with properties defined by the TheObject function. (In the above example, you could access the make of your new object by referencing theObject.make.)

thismax
  • 391
  • 1
  • 4
  • 9
  • That explains it. Thanks a lot! – DarkLightA Dec 29 '10 at 21:36
  • No problem. Glad I could help. :) Also, are you learning JS with a tutorial? It doesn't seem like they are using the best examples. If I were you I would pick up a copy of JavaScript: The Good Parts, by Douglas Crockford. – thismax Dec 29 '10 at 21:41
  • this example came from here: http://stackoverflow.com/questions/4557185/javascript-why-isnt-this-object-changed/4557522#4557522 – NickAldwin Dec 29 '10 at 21:45
2

Here, TheObject is the type of object (class) which "theObject" is. The function with the same name as the type is called a constructor. Calling it constructs a new object of that type. (e.g. for a TheObject type, new TheObject() creates a new object of the type TheObject)

Think of it this way: The function below makes myAuto a new Car object (of type "Car"):

function myNewFunc(myAuto) {
  myAuto = new Car("Audi","TT",2001);
}

(It's possible the "Object" vs "TheObject" vs "theObject" terminology is confusing you. Where are you getting this sample code?)

NickAldwin
  • 11,584
  • 12
  • 52
  • 67
  • How is this 'TheObject' different from other objects? – DarkLightA Dec 29 '10 at 21:34
  • As thismax said above, the TheObject datatype was defined somewhere else in the class. Now that I see where this came from (another SO question), I can see why it was confusing, as ivo_es did not provide the definition with his example. – NickAldwin Dec 29 '10 at 21:46