In JavaScript, methods are function that exist on an object. Here is an example…
class Animal {
constructor(name){
this.name = name;
}
grunt(){
return ${this.name} says “ugh!”;
} }
let gorilla = new Animal(‘Bongo’);
console.log(gorilla.grunt());
This code would output Bongo says “ugh!”. Notice that we did not use the function keyword. We did need the return though.
Note: You might notice that we used backticks in this example. You also might notice something odd. We used a dollar sign ($) and curly braces ({}) in our line of log code. This syntax allows for interpolation. We can put a variable inside the curly braces and the code will display the value of the variable.
Happy Coding!
Clay Hess