What does this javascript code mean?
function myFunc(theObject) {
theObject = new TheObject("Ford","Focus",2006);
}
Why is new TheObject()
u开发者_开发问答sed instead of something like new Object()
? I don't understand.
There's a function TheObject(...)
"class" somewhere this is creating that occurs before this in your included code, that's what it's creating.
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
.)
TheObject is a user defined object.
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?)
精彩评论