JavaScript — Object Constructors
Why constructors?
Hand-writing similar objects doesn't scale:
const user1 = { name: "Alice", age: 25 };
const user2 = { name: "Bob", age: 30 };
A constructor stamps out the same shape repeatedly:
function User(name, age) {
this.name = name;
this.age = age;
}
const user1 = new User("Alice", 25);
const user2 = new User("Bob", 30);
Uppercase-first names (User, Product) signal "use me with new".
this inside a constructor
When called with new, this is the freshly created object — each instance gets its own property values:
alice.name; // "Alice"
bob.name; // "Bob"
Constructor parameters & defaults
function Product(name, price, category) {
this.name = name;
this.price = price;
this.category = category;
}
const phone = new Product("Phone", 50000, "Electronics");
function User(name, age = 18) { // default parameter
this.name = name;
this.age = age;
}
new User("Alice").age; // 18
Methods: per-object vs prototype
Methods assigned inside the constructor are duplicated per object:
function User(name) {
this.name = name;
this.greet = function () { console.log(`Hello ${this.name}`); };
}
Prototype methods are shared by all instances (preferred):
User.prototype.greet = function () {
console.log(`Hello ${this.name}`);
};
alice.greet(); // works for every instance, one shared function
instanceof and .constructor
user instanceof User; // true
user.constructor === User; // true
Constructor return behavior
Normally new returns the new object. Explicitly returning an object replaces it — avoid this surprise:
function Weird() {
this.name = "Alice";
return { name: "Bob" }; // returned instead!
}
new Weird().name; // "Bob"
The forgotten-new bug
const user = User("Alice"); // no new → pollutes globals / returns undefined
Always new User(...). Classes throw automatically when invoked without new.
Validation in constructors
function User(name, age) {
if (!name) throw new Error("Name is required");
if (age < 0) throw new Error("Age cannot be negative");
this.name = name;
this.age = age;
}
Generated IDs pattern
let nextId = 1;
function Task(title) {
this.id = nextId++;
this.title = title;
}
Constructor inheritance (the old way)
function Animal(name) { this.name = name; }
Animal.prototype.speak = function () { console.log(`${this.name} makes a sound`); };
function Dog(name) {
Animal.call(this, name); // reuse parent constructor
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Modern classes make this readable — see the classes lesson.
Constructor vs factory
| Constructor | Factory | |
|---|---|---|
| Call | new User("Ada") | createUser("Ada") |
| Prototype link | yes | only if you add it |
| Forgot-new risk | yes | none |
Common mistakes: missing
new; methods duplicated instead of prototyped; surprising explicit object returns.
Mini Practice
- Build a User constructor with name/email; create three users.
- Move greet() to the prototype; verify both instances share it.
- Test with instanceof; check .constructor.
- Add default params + validation throwing on bad input.
- Create a Product constructor with auto-incrementing ids.
- Rewrite one constructor as a class; compare.
Next: prototypes →
Related Topics
Frequently Asked Questions about Object Constructors
What is Object Constructors in JavaScript?
Object Constructors is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Object Constructors?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Object Constructors.
Why is Object Constructors important in JavaScript?
Object Constructors is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.