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.
```js
function Bird(name, color) {
this.name = name;
this.color = color;
this.numLegs = 2;
this.fly = function() {
console.log(`I'm flying. I have ${this.numLegs} legs.`);
}
}
const big = new Bird('Albert', 'blue')
console.log(big) //➞
/*
Bird {
name: 'Albert',
color: 'blue',
numLegs: 2,
fly: ƒ (),
__proto__: { constructor: ƒ Bird() }
}
*/
```
This constructor defines a `Bird` object with properties `name`, `color`, and `numLegs` set to Albert, blue, and 2, 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.