← Back to list

Function Constructor

A function constructor is a regular function used to create and initialize objects. When called with the new keyword, it creates a new…

Honey Tyagi · 2025-05-23 02:17 · 0 claps · 1.6 min read
#constructor #constructor-function #object-constructor
Open on Medium ↗

Function Constructor

A function constructor is a regular function used to create and initialize objects. When called with the new keyword, it creates a new object, binds this to it, and returns it.

function Person(name, age) {
  this.name = name;
  this.age = age;
}

const john = new Person('John', 30);
console.log(john) // {name: 'john' , age: 30}

Why do we use capital letters to name constructor functions (e.g., Person) by convention?

It distinguishes constructor functions from regular functions and signals that the function should be used with new.

Return From Constructor

Usually, constructors do not have a return statement. Their task is to write all necessary stuff into this, and it automatically becomes the result.

But if there is a return statement, then the rule is simple:

  • If return is called with an object, then the object is returned instead of this.
  • If return is called with a primitive, it’s ignored.

In other words, return with an object returns that object, in all other cases this is returned.

🧪 Example: Returning an object from a constructor

function User(name) {
  this.name = name;

  return {
    name: 'Overridden',
    role: 'admin'
  };
}

const user = new User('Alice');
console.log(user);         // 👉 { name: 'Overridden', role: 'admin' }
console.log(user.name);    // 👉 Overridden

Explanation:

Inside the User constructor:

this.name = name; normally sets the property on the new instance (this). But then return { name: 'Overridden', role: 'admin' } explicitly returns an object. Because it’s an object, JavaScript returns it instead of this.

🧪 Example: Returning a primitive (like string, number)

function Animal(type) {
  this.type = type;

  return 'Dog'; // <- primitive return
}

const a = new Animal('Cat');
console.log(a);         // 👉 Animal { type: 'Cat' }
console.log(a.type);    // 👉 Cat

Explanation:

return 'Dog' is a primitive, so JavaScript ignores it.

The value of this is returned by default → Animal { type: 'Cat' }.

How would you simulate inheritance using constructor functions?

function Animal(name) {
  this.name = name;
}
Animal.prototype.speak = function() {
  console.log(`${this.name} makes a noise.`);
};

function Dog(name, breed) {
  Animal.call(this, name); // Inherit properties
  this.breed = breed;
}

Dog.prototype = Object.create(Animal.prototype); // Inherit methods
Dog.prototype.constructor = Dog;

Dog.prototype.bark = function() {
  console.log(`${this.name} barks.`);
};

const d = new Dog('Rex', 'Labrador');
d.speak(); // Rex makes a noise.
d.bark();  // Rex barks.

메타데이터
post_id
3786b32af9a2
slug
function-constructor-3786b32af9a2
url
https://medium.com/@honey9999901/function-constructor-3786b32af9a2
canonical_url
https://medium.com/@honey9999901/function-constructor-3786b32af9a2
author_url
https://medium.com/@honey9999901
status
ok
fetched_at
2026-07-28 05:39:03