When I'm explaining the difference between functions and methods, I always add, that methods are specific functions running in the context of an object.
Let's imagine, we want to let a user say hello:
// Functionconstuser={name:"Firstname Lastname"};functionsayHelloFunction(user){console.log(`Hello, my name is ${user.name}!`);}// Somewhere in your code:sayHelloFunction(user);// ----------------------------//// MethodclassUser{constructor(name){this.name=name;}sayHelloMethod(){console.log(`Hello, my name is ${this.name}!`);}}constuser=newUser("Firstname Lastname");// somewhere in your code:user.sayHelloMethod();
Note, that we don't have to pass a user to the method, since we provided that information implicitly through the context of the user-object, on which the method is called.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Great post!
When I'm explaining the difference between functions and methods, I always add, that methods are specific functions running in the context of an object.
Let's imagine, we want to let a user say hello:
Note, that we don't have to pass a user to the method, since we provided that information implicitly through the context of the user-object, on which the method is called.