Constructors are functions that create new objects. They define properties and behaviors that will belong to the new object. Think of them as a blueprint for the creation of new objects.
Here is an example of a constructor:
function Dog() {
this.name = "Anakin";
this.color = "brown";
this.numLegs = 4;
}
- This constructor defines a
Dog
object with propertiesname
,color
, andnumLegs
set to Anakin, brown, and 4, respectively. Constructors follow a few conventions:
Constructors are defined with a capitalized name to distinguish them from other functions that are not
constructors
.Constructors use the keyword
this
to set properties of the object they will create. Inside the constructor,this
refers to the new object it will create.Constructors define properties and behaviors instead of returning a value as other functions might.
Top comments (0)
Some comments have been hidden by the post's author - find out more