Function Constructors and Closures in JavaScript
Function constructors can use closures for private methods. Here is how and why modern code uses classes instead.
Function Constructors and Closures in JavaScript
Before ES6 classes, JavaScript used function constructors to create objects. Closures can be used inside them to create private methods.
Basic Function Constructor
function Person(name, age) { this.name = name; this.age = age; } Person.prototype.greet = function () { return `Hello, I am ${this.name}`; }; const p = new Person("Kunal", 30); p.greet(); // "Hello, I am Kunal" `` Properties on `this` are public. Methods on `prototype` are shared across instances. ### Private Variables With Closures ```js function Person(name, age) { this.name = name; // public let _age = age; // private (closure) this.getAge = () => _age; this.setAge = (newAge) => { _age = newAge; }; } const p = new Person("Kunal", 30); p.getAge(); // 30 p._age; // undefined (private) p.setAge(31); p.getAge(); // 31 `` `_age` is in the constructor's local scope. `getAge` and `setAge` close over it. It is not accessible as a property. ### Private Methods With Closures ```js function Counter() { let count = 0; function validate(n) { return n >= 0; } this.increment = () => { if (validate(1)) count++; return count; }; this.decrement = () => { if (validate(-1)) count--; return count; }; } const c = new Counter(); c.increment(); // 1 c.validate; // undefined (private method) `` `validate` is a private method. It is only accessible inside the constructor and the methods that close over it. ### The Problem: Methods on `this` Are Not Shared When you put methods on `this` inside the constructor (as arrow functions), each instance gets its own copy. This uses more memory than prototype methods. ```js function Person(name) { this.name = name; this.greet = () => `Hi, ${this.name}`; // new function per instance } // vs function Person(name) { this.name = name; } Person.prototype.greet = function () { return `Hi, ${this.name}`; }; // shared `` The prototype version is more memory-efficient. But it cannot close over private constructor variables. ### Modern Alternative: ES6 Classes With `#private` ```js class Person { #age; // private field constructor(name, age) { this.name = name; this.#age = age; } getAge() { return this.#age; } setAge(newAge) { this.#age = newAge; } } `` Classes with `#private` fields provide true privacy without the memory cost of per-instance methods. ### The Takeaway Function constructors can use closures for private variables and methods, but per-instance methods cost memory. ES6 classes with `#private` fields are the modern way to achieve true privacy efficiently. Use function constructors only in old codebases.
By declaring variables with let or var inside the constructor and creating methods on this that close over them. The variables are in the constructor's local scope and are not accessible as properties, only through the methods.
Methods created inside the constructor (on this) are per-instance, not shared on the prototype. Each instance gets its own copy, which uses more memory than prototype methods. ES6 classes with #private fields solve this.
ES6 classes with #private fields (ES2022). They provide true privacy without the memory cost of per-instance methods. Private fields are not accessible outside the class, and methods are shared on the prototype.
Yes. Declare a function inside the constructor (not on this). It is only accessible inside the constructor and to methods that close over it. It is not accessible as a property on instances.
Because prototype methods are shared across all instances (one copy). Methods created inside the constructor (on this) are per-instance (one copy per instance). For many instances, the memory difference is significant.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

