JavaScript Object Constructors
Sometimes we need to create many objects of the same type.
Object Constructor Functions
Sometimes we need to create many objects of the same type.
To create an object type we use an object constructor function.
It is considered good practice to name constructor functions with an upper-case first letter.
Object Type Person
Example Code
javascriptfunction Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}Now we can use new Person() to create many new Person objects:
Example
Example Code
javascriptconst myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");
const mySister = new Person("Anna", "Rally", 18, "green");
const mySelf = new Person("Johnny", "Rally", 22, "green");Property Default Values
A value given to a property will be a default value for all objects created by the constructor:
Example
Example Code
javascriptfunction Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "English";
}Adding a Property to an Object
Adding a property to a created object is easy:
Example
Example Code
javascriptmyFather.nationality = "English";Adding a Property to a Constructor
You can NOT add a new property to an object constructor:
Example
Example Code
javascriptPerson.nationality = "English";To add a new property, you must add it to the constructor function prototype:
Example
Example Code
javascriptPerson.prototype.nationality = "English";Constructor Function Methods
A constructor function can also have methods:
Example
Example Code
javascriptfunction Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.fullName = function() {
return this.firstName + " " + this.lastName;
};
}Adding a Method to an Object
Adding a method to a created object is easy:
Example
Example Code
javascriptmyMother.changeName = function (name) {
this.lastName = name;
}Adding a Method to a Constructor
You cannot add a new method to an object constructor function.
This code will produce a TypeError:
Example
Example Code
javascriptPerson.changeName = function (name) {
this.lastName = name;
}
myMother.changeName("Doe");Adding a new method to a constructor must be done to the constructor function prototype:
Example
Example Code
javascriptPerson.prototype.changeName = function (name) {
this.lastName = name;
}
myMother.changeName("Doe");Built-in JavaScript Constructors
JavaScript has built-in constructors for all native objects:
Example
Example Code
javascriptnew Object() // A new Object object
new Array() // A new Array object
new Map() // A new Map object
new Set() // A new Set object
new Date() // A new Date object
new RegExp() // A new RegExp object
new Function() // A new Function objectDid You Know?
Use object literals {} instead of new Object().
Use array literals [] instead of new Array().
Use pattern literals /()/ instead of new RegExp().
Use function expressions () {} instead of new Function().
Example
Example Code
javascript""; // primitive string
0; // primitive number
false; // primitive boolean
{}; // object object
[]; // array object
/()/ // regexp object
function(){}; // functionInteractive Code Examples
Object Type Person
javascriptfunction Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eye;
}JS Example 2
javascriptconst myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");
const mySister = new Person("Anna", "Rally", 18, "green");
const mySelf = new Person("Johnny", "Rally", 22, "green");JS Example 3
javascriptfunction Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "English";
}JS Example 4
javascriptmyFather.nationality = "English";JS Example 5
javascriptPerson.nationality = "English";JS Example 6
javascriptPerson.prototype.nationality = "English";