JavaScript Instance Methods
Instance methods are functions intended to operate on individual objects created from a class or constructor. In JavaScript classes, these methods are normally stored on the class prototype and shared by instances rather than copied onto each instance.
An instance method is declared inside a JavaScript `class` without `static`. When an instance calls that method, JavaScript finds the function through the object's prototype chain. This lets many instances reuse one function while keeping their own instance-specific data.
Inside a normal instance method, `this` refers to the object on which the method was called, assuming the method is invoked with normal method-call syntax. For example, `user.greet()` gives the method a `this` value referring to `user`.
Class syntax hides much of the prototype machinery, but the underlying model remains prototype-based. A method such as `greet()` is generally found on `User.prototype`, while properties assigned in the constructor, such as `this.name`, belong directly to each instance.
Instance methods differ from `static` methods. A static method belongs to the class itself and is called with `User.method()`, whereas an instance method is called on an object such as `user.method()`. Instance methods are appropriate when the operation depends on the state of a particular object.
Key points
- Methods declared in a class without `static` are instance methods.
- Class instance methods are normally stored on the class prototype and shared by instances.
- `this` normally refers to the instance used to invoke the method.
- Instance data such as `this.name` is typically stored directly on each object.
- Static methods belong to the class, not individual instances.
Interview questions
Where are JavaScript class instance methods stored?
Normal instance methods declared in a class are placed on the class's prototype rather than copied onto every instance. Instances can find those methods through their prototype chain. This allows multiple objects to share the same function. Instance-specific values such as `this.name` can still live directly on each object.
What does `this` mean inside an instance method?
With ordinary method-call syntax, `this` refers to the object used to make the call. Thus, `user.greet()` makes `this` refer to `user`. The same function can therefore operate on different instances. If the method is detached from the object, its `this` behavior can change.
Why are instance methods shared through the prototype instead of copied to every object?
Sharing methods avoids creating a separate function object for every instance. All instances can resolve the same function through their prototype. The objects still retain independent state in their own properties. This separates per-object data from reusable behavior.
How would you distinguish an instance method from a static method in a code review?
An instance method is declared without `static` and is normally called through an instance, such as `user.greet()`. A static method is declared with `static` and is called through the class, such as `User.create()`. The instance method is designed to work with a particular object's state. A static method is associated with the class itself rather than a particular instance.