JS: Constructor

By Xah Lee. Date: . Last updated: .

What is a Constructor

Constructor is a function that is designed to create a object that is of specific type. (e.g. create a date object, array object, etc.)

Constructor is typically called with new (operator)

Builtin Constructors

example of standard builtin constructors.

Primitive Value Wrapper Constructors

The following are constructors for Primitive Value 's wrapper objects.

User Defined Constructor

Parenthesis Optional for Constructor Call with No Args

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

e.g. equivalent:

Do Not Confuse with Property Name “constructor”

Do not confuse constructor with property key "constructor".

💡 TIP: Use Literal Expression When Possible

For builtin objects, you should use the literal expression to create object whenever possible. Because:

💡 TIP: Constructor Name Start with Capital Letter

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

JavaScript. Constructor, Class