Note: In all the examples below, the normal anonymous function notation (
function(a, b) {...}
) may be replaced by the new ES6 arrow notation: (a,b) => {...}
.JavaScript Constructors
Constructors are basically objects that can be created by functions.
For example, even though I said don't use it, the Object() constructor is an example of a default constructor in JavaScript
/*Not recommended*/
var foo = new Object({a: "b"}); /*<-- How to use any constructor*/
/*Literal notation (recommended)*/
var foo = {a: "b"};
Here is an example of a constructor function:
function Person(fName, lName) {
this.firstName = fName;
this.lastName = lName;
this.fullName = function() {return this.firstName + " " + this.lastName;};
}