User-defined constructors
Provides a reference of user-defined constructors.
In addition to the Object constructor, any user-defined function can be used as an object constructor.
| Syntax | Effect |
|---|---|
| new function(arg1, ..., argn) | Creates a new object, then calls function(arg1, ...,
argn) to initialize it. |
Inside the constructor, the keyword this can be
used to make reference to the object being initialized.
For example, the following program defines a constructor for cars:
function Car(name, year) {
this.name = name;
this.year = year;
this.start = start_engine;
}
Now, calling
new Car("Ford", "1985")
creates a new object with the properties name and year, and a start method.