Method definitions: How to Define Methods for JS Classes

A method is a function that is associated with an object, or, simply put, a method is a property of an object that is a function. Methods are defined the way normal functions are defined, except that they have to be assigned as the property of an object.

Why use methods?

Methods are useful for the following reasons:

  • They provide a way to group related functionality together and organize code better.
  • They allow access to the object's properties and other methods using the this keyword.
  • They can be inherited by other objects and classes, which promotes code reuse and polymorphism.

How to define methods?

There are different ways to define methods in JavaScript, depending on the context and the syntax. Here are some of the common ways to define methods:

Object methods

An object method is a property of an object that is a function. You can define an object method using the following syntax:

Javascript
                        
const obj = {
  // define a method using a function expression
  method1: function() {
    // method body
  },
  // define a method using a shorthand syntax (ES6+)
  method2() {
    // method body
  },
  // define a method using an arrow function (ES6+)
  method3: () => {
    // method body
  }
};

You can access and invoke an object method using the dot notation (obj.method1()) or the bracket notation (obj['method1']()).

Note: When using an arrow function to define a method, the this keyword will not refer to the object itself, but to the surrounding scope. This can be useful in some cases, but it can also cause unexpected behavior. Therefore, it is recommended to use a function expression or a shorthand syntax when defining object methods.

Class methods

A class method is a function that is defined inside a class. You can define a class method using the following syntax:

Javascript
                        
class ClassName {
  // define a constructor method
  constructor() {
    // constructor body
  }
  // define an instance method
  method1() {
    // method body
  }
  // define a static method
  static method2() {
    // method body
  }
}

You can access and invoke a class method depending on whether it is an instance method or a static method. An instance method is a method that belongs to an instance of a class, and can be accessed using the dot notation (instance.method1()). A static method is a method that belongs to the class itself, and can be accessed using the dot notation (ClassName.method2()).

Note: Static methods are not inherited by the instances of the class, and cannot access the instance properties or methods using the this keyword. Static methods are usually used to create utility functions that do not depend on the state of the object.

Prototype methods

A prototype method is a function that is defined on the prototype of an object or a class. The prototype is an object that is shared by all the instances of the object or the class, and is used to define properties and methods that can be inherited. You can define a prototype method using the following syntax:

Javascript
                        
// define a prototype method for an object
obj.prototype.method1 = function() {
  // method body
};

// define a prototype method for a class
ClassName.prototype.method2 = function() {
  // method body
};

You can access and invoke a prototype method using the dot notation (instance.method1() or instance.method2()), as long as the instance inherits from the prototype. Prototype methods can access the instance properties and methods using the this keyword.

Note: Prototype methods are not directly visible on the instance, but they are accessed through the prototype chain, which is a series of links between the instance and its prototypes. Prototype methods are usually used to extend the functionality of existing objects and classes, without modifying them directly.

Summary

In this article, you learned about method definitions in JavaScript, which are functions that are associated with objects or classes. You learned about the different ways to define methods, such as object methods, class methods, and prototype methods, and how to access and invoke them. You also learned about the advantages of using methods, such as grouping related functionality, accessing object properties, and inheriting methods from other objects and classes.