A variable is just a means to store and reference a value. An object is a type of value. A function is a type of object. A constructor function is a function designed to be called with the new
keyword (which creates an object and sets up the prototype chain on it).
To call a constructor function, you need to have access to that value. This can be via a variable (and usually is).
The variable must contain the constructor function before you can reference it.
A function declaration is a means to create a function, which can be a constructor function, which is hoisted, allowing it to be used earlier in the code then it appears.
However, constructor functions typically have a number of methods added to the prototype and these are not hoisted so in the example below:
- The instance of dog is successfully constructed
- The attempt to bark fails because the assignment to prototype.bark hasn't happened yet
var fido = new Dog("Fido");
fido.bark();
function Dog(name) {
this.name = name;
}
Dog.protype.bark = function () {
alert(`Woof! I'm ${this.name}`);
}