The Constructor Property
What you'll learn: How every JavaScript instance automatically has a constructor property that points back to the function that created it.
What Is the Constructor Property?
When you create an object using a constructor function and the new operator, that object inherits a special property called constructor from the constructor's prototype. This property references the constructor function itself, creating a two-way connection: the constructor has a prototype, and instances created from it have a constructor property pointing back.
How It Works
When you write new Person(), the new instance doesn't get its own constructor property. Instead, it inherits one from Person.prototype. This inherited property points back to the Person function itself.
function Dog(name) {
this.name = name;
}
const buddy = new Dog('Buddy');
console.log(buddy.constructor === Dog); // true
console.log(Dog.prototype.constructor === Dog); // true
The constructor property lives on the prototype, not on the instance. You can verify this:
console.log(buddy.hasOwnProperty('constructor')); // false
console.log(Dog.prototype.hasOwnProperty('constructor')); // true
Why It's Useful
The constructor property lets you identify which function created an object, even if you don't have direct access to that function. You can also use it to create new instances of the same "type":
const max = new buddy.constructor('Max');
console.log(max.name); // 'Max'
console.log(max.constructor === Dog); // true
Key Takeaway: The constructor property is inherited from a constructor's prototype and provides a reference back to the constructor function, creating a circular relationship between constructors, their prototypes, and instances.