Difference between Prototype Method and Instance Method and Local Function
JavaScript Prototype Method Instance Method and Local Function

Difference between Prototype Method and Instance Method and Local Function

Here’s a breakdown of the differences between a Prototype Method, an Instance Method, and a Local Function in JavaScript:

1. Prototype Method

- Definition: A method that is defined on the prototype of a constructor function or class.

- Scope: Shared across all instances of the object, and accessed via the prototype chain.

- Memory Usage: Memory-efficient, as the method is stored once on the prototype and reused by all instances.

- Best Use Case: For methods that do not need to be unique to each instance, such as common or shared functionality.

- Example:

  function Car(brand, model) {
    this.brand = brand;
    this.model = model;
  }
  Car.prototype.getDetails = function() {
    return ${this.brand} ${this.model};
  };
  const car1 = new Car("Toyota", "Camry");
  const car2 = new Car("Honda", "Civic");
  console.log(car1.getDetails()); // "Toyota Camry"
  console.log(car2.getDetails()); // "Honda Civic"
  console.log(car1.getDetails === car2.getDetails); // true (shared method)        

2. Instance Method

- Definition: A method that is attached to "this" inside a constructor function or is defined as a property within a class's constructor.

- Scope: Unique to each instance; every object gets its own copy of the method.

- Memory Usage: Less memory-efficient compared to a prototype method because a new copy of the method is created for each instance.

- Best Use Case: For methods that need to be instance-specific or require unique functionality per object.

- Example:

  function Car(brand, model) {
    this.brand = brand;
    this.model = model;
    this.getDetails = function() { // Instance-specific method
      return ${this.brand} ${this.model};
    };
  }
  const car1 = new Car("Toyota", "Camry");
  const car2 = new Car("Honda", "Civic");
  console.log(car1.getDetails()); // "Toyota Camry"
  console.log(car2.getDetails()); // "Honda Civic"
  console.log(car1.getDetails === car2.getDetails); // false (different methods)        

3. Local Function

- Definition: A function declared within another function (like a constructor) but not attached to the instance (`this`) or the prototype. It exists only within the scope of the enclosing function and is not accessible outside.

- Scope: Limited to the function in which it is defined; does not exist on the instance or the prototype.

- Memory Usage: Independent of instances, but cannot be reused since it is scoped locally.

- Best Use Case: For utility functions or calculations used internally within a constructor that don’t need to be exposed.

- Example:

  function Car(brand, model) {
    this.brand = brand;
    this.model = model;
    function localFunction() { // Only accessible inside the function Car
      return ${brand} ${model};
    }
    this.getDetails = function() {
      return localFunction(); // Use the local function internally
    };
  }
  const car = new Car("Toyota", "Camry");
  console.log(car.getDetails()); // "Toyota Camry"
  // console.log(car.localFunction()); // Error: localFunction is not defined        

Summary

- Use prototype methods for shared, common functionality to save memory.

- Use instance methods if each instance needs its own version of the method.

- Use local functions for utility code that doesn’t need to be accessible from the instance or prototype.

Ask for any questions in the comments. Thanks !!!

要查看或添加评论,请登录

Aabhas Saxena的更多文章

社区洞察