5.5 Constructor Properties
Every object in JavaScript has a constructor property that points to the function used to build or create that object. Take two circles, one created with our custom Circle constructor, the other with the object literal syntax. Log other.constructor in the console and you see our Circle function — that is the function that created the object.
Now log circle.constructor for the one created with object literal syntax: you get a built-in function whose name starts with a capital letter — Object. Whenever you write something like const x = {};, the JavaScript engine internally translates it into const x = new Object();. The object literal syntax is just a friendly shortcut over the built-in Object constructor.
Other built-in constructors
String— we usually prefer string literals like'hi',"hi"or template literals`hi`.Boolean— we prefer the literalstrueandfalse.Number— we just write numeric literals like42.
Behind the scenes, all of these literals use their respective built-in constructors, but the literal forms are shorter and clearer, which is why we use them in everyday code. The takeaway is simple: every JavaScript object exposes a constructor property that points back to the function used to instantiate it. That is all for this short lesson — see you in the next video.
Summary
This lesson explores the constructor property, which exists on every JavaScript object and references the function used to create that object. The video explains built-in constructors like Object, String, Boolean, and Number, and demonstrates how JavaScript internally translates object literal syntax (e.g., `{}`) into a call to the Object constructor. It highlights that while literal syntax is cleaner and more commonly used than calling constructors directly, understanding the underlying constructor mechanism is fundamental to object creation.
Key points
- Every object in JavaScript has a constructor property that references the function used to create it
- Built-in constructors include Object, String, Boolean, and Number; JavaScript automatically uses these behind the scenes
- Object literal syntax `{}` is internally translated by the engine to `new Object()`, using the Object constructor
- While object, string, boolean, and number constructors exist, using literal syntax (literals) is cleaner and preferred
- Understanding the constructor property reveals how JavaScript handles object creation and the relationship between literals and their underlying constructor functions
FAQ
What is the constructor property?
The constructor property is a property present on every JavaScript object that references the function which was used to create or instantiate that object. For example, if you create an object using a Circle constructor function, the object's constructor property will point back to the Circle function.
How does JavaScript handle object literal syntax internally?
When you create an object using literal syntax (e.g., `const obj = {}`), JavaScript internally translates this into a constructor call like `new Object()`. This means the Object constructor function is used under the hood, and the created object's constructor property will reference this built-in Object function.
Why should I use literals instead of constructor functions for strings, booleans, and numbers?
Literals (simple strings like `'hello'`, booleans like `true`/`false`, and numeric values like `42`) are cleaner, simpler, and more readable than explicitly calling constructors like `new String()`, `new Boolean()`, or `new Number()`. Literals are the preferred approach in modern JavaScript, even though the constructors exist.