Difference between function and method keywords
Lukas Polak
Posted on September 12, 2020
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!"
💖 💪 🙅 🚩
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
webdev Understanding HTTP, Cookies, Email Protocols, and DNS: A Guide to Key Internet Technologies
November 30, 2024
softwareengineering Git Mastery: Essential Questions and Answers for Developers 🚀
November 30, 2024