Difference between function and method keywords

lukaspolak

Lukas Polak

Posted on September 12, 2020

Difference between function and method keywords

A method is a function that belongs to the object. Methods have a "receiver" while functions do not.

const someObject = {
  someMethod: function () {
    console.log('Hello, World!');
  },
};

// method call
someObject.someMethod(); // `someObject` is a receiver, `someMethod` is a method

const someFunction = someObject.someMethod;

// function invocation
someFunction(); // JavaScript functions are invoked without a receiver, using parenthesis syntax

function helloWorld() {
  console.log('Hello, World!');
}

helloWorld(); // logs "Hello, World!"

// When a function is declared on the global scope, it becomes a property of a global object
// The `window` is the receiver of `helloWorld,` which would make `helloWorld` a method and not a function. However, in JavaScript, it is still considered a function.
window.helloWorld(); // logs "Hello, World!"
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
lukaspolak
Lukas Polak

Posted on September 12, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related