JavaScript对象属性内存分配及私有方法内存效率问题咨询
1. When creating new objects in JavaScript, do all properties of a function get allocated new memory space?
Great question! The short answer is: it depends on the type of property and where it’s defined. Let’s break this down clearly:
Primitive values (strings, numbers, booleans, etc.): When you define these as instance properties inside a constructor or class, every new object gets its own copy in memory. For example:
function User(username) { this.username = username; // Each User instance has its own username (new memory allocated) }Each
Userinstance stores a separateusernamevalue, so fresh memory is used for each.Function properties (methods): This is where the efficiency difference matters:
- If you define a method inside the constructor, every instance gets a brand new function object. That means each instance uses extra memory for its own copy of the method:
function User(username) { this.username = username; this.greet = function() { // New function created for every User instance console.log(`Hello, ${this.username}!`); }; } - If you define the method on the constructor’s prototype, all instances share the same function reference. Only one copy of the method exists in memory, no matter how many instances you create:
User.prototype.greet = function() { // Single function shared by all User instances console.log(`Hello, ${this.username}!`); };
Modern class syntax does this automatically for methods defined in the class body (not inside the constructor):
class User { constructor(username) { this.username = username; } greet() { // Prototype method, shared across all instances console.log(`Hello, ${this.username}!`); } }- If you define a method inside the constructor, every instance gets a brand new function object. That means each instance uses extra memory for its own copy of the method:
To sum up: Primitive instance properties get new memory per instance, but prototype methods are shared and only take up memory once.
2. Why is displayIncreasedSalary more efficient than increaseSalary (based on common private method examples)?
You’re exactly right that private methods implemented via closures or instance-bound functions can be memory-heavy—each instance gets its own copy. Let’s use a typical example to explain the efficiency gap:
Suppose we have an Employee class where:
increaseSalaryis a private method assigned inside the constructor (instance-bound)displayIncreasedSalaryis a prototype method defined in the class body
class Employee { #salary; // Private class field constructor(salary) { this.#salary = salary; // Instance-bound method: new copy created for every Employee instance this.increaseSalary = () => { this.#salary *= 1.1; }; } // Prototype method: single copy shared by all instances displayIncreasedSalary() { this.increaseSalary(); console.log(`New salary: $${this.#salary.toFixed(2)}`); } }
Here’s why displayIncreasedSalary is more efficient:
increaseSalary: Since it’s assigned inside the constructor, every newEmployeeinstance creates a fresh function object. If you have 1000Employeeinstances, that’s 1000 separate copies ofincreaseSalaryeating up memory.displayIncreasedSalary: This method lives onEmployee.prototype, so all instances reference the exact same function. No matter how many instances you create, only one copy exists in memory.
If you want private methods without the memory overhead, modern JavaScript has private prototype methods (using the # syntax) that combine privacy and efficiency:
class Employee { #salary; constructor(salary) { this.#salary = salary; } // Private prototype method (single copy, shared by all instances) #increaseSalary() { this.#salary *= 1.1; } displayIncreasedSalary() { this.#increaseSalary(); console.log(`New salary: $${this.#salary.toFixed(2)}`); } }
Now #increaseSalary is private but only exists once in memory, making it just as efficient as displayIncreasedSalary.
内容的提问来源于stack exchange,提问作者pragya91

