Reputation: 15692
function myFunc(theObject) {
theObject = new TheObject("Ford","Focus",2006);
}
Why is new TheObject()
used instead of something like new Object()
? I don't understand.
Upvotes: -1
Views: 157
Reputation: 11754
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?)
Upvotes: 2
Reputation: 381
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
.)
Upvotes: 3
Reputation: 630637
There's a function TheObject(...)
"class" somewhere this is creating that occurs before this in your included code, that's what it's creating.
Upvotes: 4