Suggest an editImprove this articleRefine the answer for “How do you add a method to a class?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)You can add a method to a class in several ways: a **regular method** is declared right in the class body and ends up on `User.prototype`, so it's shared by every instance; you can also add one manually via `ClassName.prototype.method = ...` after the class has been declared. **Key point:** besides a regular method there are also static methods (`static`), getters/setters, private methods (`#method`), and field methods written as arrow functions, which don't lose their `this` context even when passed as a callback.Shown above the full answer for quick recall.Answer (EN)Image## 1. Regular (Public) Method A method available on every instance of the class: ```javascript class User { constructor(name) { this.name = name; } greet() { console.log(`Hi, ${this.name}!`); } } const u = new User("Alice"); u.greet(); // Hi, Alice! ``` Such methods are automatically added to `User.prototype`, meaning they are **shared across all instances** (rather than being recreated for every object). --- ## 2. Adding a Method **After the Class Is Declared** You can add a method to a class's prototype manually, after it has been declared: ```javascript class User { constructor(name) { this.name = name; } } // Add a method afterward User.prototype.sayBye = function () { console.log(`${this.name} says: bye!`); }; const u = new User("Alice"); u.sayBye(); // Alice says: bye! ``` This works because under the hood, a `class` is just a function with a `prototype`. --- ## 3. Static Method (`static`) This kind of method is called **directly on the class**, not on an instance: ```javascript class MathUtils { static square(n) { return n * n; } } console.log(MathUtils.square(5)); // 25 ``` > Instances (`new MathUtils()`) do not have access to `static` methods. --- ## 4. Getter and Setter Used to "disguise" properties as methods: ```javascript class Product { constructor(price) { this._price = price; } get price() { return this._price + " $"; } set price(value) { if (value < 0) throw new Error("Price cannot be negative"); this._price = value; } } const p = new Product(1000); console.log(p.price); // "1000 $" p.price = 1500; console.log(p.price); // "1500 $" ``` > Accessing `p.price` actually calls `get price()`, > assigning `p.price = ...` calls `set price()`. --- ## 5. Private Methods (`#methodName`) Inaccessible from outside, only from within the class: ```javascript class Account { #logBalance() { console.log(`Current balance: ${this.balance}`); } constructor(balance) { this.balance = balance; this.#logBalance(); // can only be called from here } } const acc = new Account(500); // acc.#logBalance(); Error: private method ``` --- ## 6. Field Methods (Arrow Functions) If you want a method to **preserve the `this` context**, you can declare it as a field: ```javascript class Button { label = "Click me"; handleClick = () => { console.log(`Clicked: ${this.label}`); }; } const b = new Button(); b.handleClick(); // Clicked: Click me ``` Such methods don't lose `this`, even when passed as a callback: ```javascript setTimeout(b.handleClick, 1000); // everything works correctly ``` --- ## Summary | Method Type | Syntax | Accessibility | Notes | | --- | --- | --- | --- | | Regular | `method() {}` | Via an instance | Shared by all instances | | Static | `static method() {}` | Via the class itself | Not visible on instances | | Getter / setter | `get name() {}`, `set name(v) {}` | Like a property | Access can be controlled | | Private | `#method() {}` | Only inside the class | Full encapsulation | | Field method (arrow) | `method = () => {}` | Via an instance | Doesn't lose `this` |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.