JavaScript Constructors
A constructor is a function that executes the initialization of an object when the object is created.
Arguments are passed to the constructor function when you use the new
keyword. There are two types of constructors,
one defined as a function
, and one defined with the constructor
method in a class
.
The Object Constructor Function
To use a constructor function to create objects, simply define a JavaScript function with any number of arguments.
Inside the function, the keyword this
is used as a placeholder for the object being created.
If the constructor function is used by itself, without the new
statement, this
will have no value.
It’s preferred to capitalize a constructor function.
Example
function Car(make, model, year) {this.make = make;this.model = model;this.year = year;}var car1 = new Car('Chevy', 'Blazer', 2015);var car2 = new Car('Ford', 'Taurus', 2018);console.log('Car 1 is a ' + car1.year + ' ' + car1.make + ' ' + car1.model);// Output: Car 1 is a 2015 Chevy Blazerconsole.log('Car 2 is a ' + car2.year + ' ' + car2.make + ' ' + car2.model);// Output: Car 2 is a 2018 Ford Taurus
Codebyte Example
Here is a codebyte example that demonstrates the usage of the constructor
function:
The Class constructor
Method
When defining a class
in JavaScript, there is a special constructor
method defined within it.
Like the object constructor function, the constructor
method can be defined with any number of arguments,
and the this
keyword can be used as a placeholder for the object being created.
Example
class Car {constructor(make, model, year) {this.make = make;this.model = model;this.year = year;}}var car1 = new Car('Chevy', 'Blazer', 2015);var car2 = new Car('Ford', 'Taurus', 2018);console.log('Car 1 is a ' + car1.year + ' ' + car1.make + ' ' + car1.model);// Output: Car 1 is a 2015 Chevy Blazerconsole.log('Car 2 is a ' + car2.year + ' ' + car2.make + ' ' + car2.model);// Output: Car 2 is a 2018 Ford Taurus
Codebyte Example
Here is a codebyte example that illustrates the use of the constructor
method:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Skill path
Create a Back-End App with JavaScript
Learn how to build back-end web APIs using Express.js, Node.js, SQL, and a Node.js-SQLite database library.Includes 8 CoursesWith CertificateBeginner Friendly30 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours