I come from a Java background, completely understand what constructor does in java. I am new to javascript and trying to understand why a constructor
is called constructor function
. Do constructors
and constructor functions
mean different things in javascript ?

- 5,320
- 16
- 79
- 132
-
Maybe this answer will help, I've tried to explain how JavaScript constructor functions and the prototype works: http://stackoverflow.com/a/16063711/1641941 A constructor function is just like any other function but you invoke it with the `new` keyword so it's used as a constructor for an object. – HMR Feb 21 '14 at 01:20
3 Answers
A constructor in JavaScript is a function that is used to construct an object. For example:
function Obj() {
// The constructed object is bound to the 'this' variable
this.property = "someValue";
}
var obj = new Obj();
console.log(obj.property); // Will print "someValue"
console.log(obj.constructor); // Will print Obj's code
Obj
is now the constructor of the instance obj
. Obj is just a normal function, but since it's invoked via new
it acts as a constructor.
Note that constructor functions are by convention named like Java classes, i.e. Upper CamelCase. The language doesn't enforce it in any way, but it makes it clear that a function is intended as a constructor.
You can also achieve inheritance in JavaScript, even though the language lacks classes per se, by defining constructors' "prototypes":
// Base constructor, like a baseclass in Java
function Base() {
this.baseProperty = "baseValue";
}
// Base implementation of a method
Base.prototype.printSelf = function () {
console.log(this.baseProperty);
};
// Constructor, corresponding to a subclass in Java
function Obj() {
// Call "baseclass" constructor
Base.call(this);
this.property = "someValue";
}
// Inherit prototype from Base
Obj.prototype = Object.create(Base.prototype);
// Overload printSelf
Obj.prototype.printSelf = function () {
// Call base implementation
Base.prototype.printSelf.call(this);
console.log(this.property);
};
var obj = new Obj();
obj.printSelf();
See my fiddle for a live demonstration if you like.

- 65,625
- 67
- 195
- 317
Java constructors are special functions that can only be called at object creation; JavaScript "constructors" are just standard functions.

- 4,586
- 2
- 22
- 31
-
1Do constructors and constructor functions mean different things in javascript ? – JavaDeveloper Feb 21 '14 at 00:41
-
Well, since "JavaScript "constructors" are just standard functions", there is no difference between a constructor and a constructor function. – ltalhouarne Feb 21 '14 at 00:48
-
1Worth mentioning - not all JS functions are (can be) constructors. – Benjamin Gruenbaum Feb 21 '14 at 01:02
JS has no special handling or type called function constructor. It's a term used to imply that this function is used with the new keyword to create an object. Any function can be a "function constructor" by prefixing it with the new keyword.
Of course that is usually not the intention of the author. So the convention is to uppercase the first letter of the function name to denote that is a function constructor and should therefore be used in conjuction w/ new.
The term constructor is usually meant to mean function constructor. Most in the community use the term full term since JS has no classes, and function constructor is more clear.

- 537
- 4
- 13