JavaScript: What's Constructor?

By Xah Lee. Date: . Last updated: .

What's a Constructor?

When a function is designed to be called with the keyword new, for example new function_name(…), we call the function constructor. ECMAScript 2015 §Overview#sec-constructor

Constructor is one of the way to create a object. 〔►see JavaScript: Creating Object

Parenthesis Optional for Constructor Call with No Args

When a function is used as a constructor and without argument, the parenthesis are optional. Example:

// the following are equivalent
var xx = new F();
var xx = new F;

Builtin Constructors

The most common use of new is to create a builtin object using builtin constructor, such as Date object.

// create a new Date object
var x = new Date();
console.log(x); // Mon, 05 Aug 2013 02:26:25 GMT
JavaScript Construtors
ObjectLiteral Expression SyntaxConstructor SyntaxNotes
Generic Object{…}new Object(…)〔►see JavaScript: What's Object?
Array[…]new Array(…)〔►see JavaScript: Understand JS Array
Functionfunction (…) {…}new Function(…)〔►see JavaScript: Function Constructor
Regex/…/…new RegExp(…)〔►see JavaScript: RegExp.prototype
Datenonenew Date(…) 〔►see JavaScript: Date Object
Mathnonenone 〔►see JavaScript: Math Object
JSONnonenone 〔►see JavaScript: JSON Object

Primitive Value Wrapper Constructors

The following are constructors for primitive values's wrapper objects. (when you call a method on a primitive value, such as "a/b".split("/"), the string is temporarily converted to a string object. That's why it has methods.)

〔►see JavaScript: Data Types

〔►see JavaScript: Primitive Value Object Wrapper

Constructor for Primitive Values's Wrapper Objects
ObjectLiteral Expression SyntaxConstructor SyntaxNotes
Number{3, -3, 3.7, …}new Number(…)Wrapper. To see all its properties, use Object.getOwnPropertyNames(Number).
String"…"new String("…")Wrapper. 〔►see JavaScript: String.prototype
Boolean{true, false}new Boolean(…)Wrapper. 〔►see JavaScript: What's True, False?〕 〔►see JavaScript: Boolean Object

For builtin objects, you should use the literal expression whenever possible.

Constructor Name Should Start with Capital Letter

By convention, functions designed to be used as constructor starts with a capital letter.

This is important, because, function designed as constructor usually does not contain return statement. So, using a constructor function without new may not do anything.

Function starting with capital letter lets you know whether it is designed to be called with new.

Though, you can write a function such that f(…) and new f(…) have the same behavior. (just have a return statement and have it return a object.) The builtin function Array() is like that. 〔►see JavaScript: Create Array

User Defined Constructor

ANY function user creates can be called with the new keyword. It will return a object.

Exactly what object new f(…) returns depends on: ① f's definition contains a return statement, and ② that statement returns a value that is a object data type. If both conditions are met, then that return object is returned. Otherwise, a newly created object is returned.

See: JavaScript: Operator “new”.

JS Constructor Topic

  1. JavaScript: Keyword “this”
  2. JavaScript: What's Constructor?
  3. JavaScript: Property Key "prototype"
  4. JavaScript: Operator “new”
  5. JavaScript “instanceof” Operator
  6. JavaScript: How to Use Object.create() to Emulate Constructor?
  7. JavaScript: Property Key "constructor"
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.