+ 4
Difference between function and method?
2 Answers
+ 8
The way I think about it is that a method is a function of an object. For instance:
var player = {
x:10,
y:30,
show:function(){
//draw the player
}
};
Here, when you call player.show(), you're calling a method of the player instance.
Hope it helps :)
+ 1
Good question! Here is what I found out:
In JavaScript,
Method is something that an object can DO or something that can be done to that object. It is like a verb.
Examples:
write(); ----print on screen----
alert(); ----alert the user-----
log(); -----log something to the console-----
Objects and method are connected with dot (.). the first word is object and the second word is method.
Examples:
document.write();
window.alert(); // word window is omitted because it is default. Used when needed.
console.log();
In above examples, you are basically saying write in the document or log in console or alert in window.
I can see why Method and Function can be confusing because of parentheses and I was confused at first too.
Like Function, you can have parameters or an argument inside it.
Big difference is Function is a grouping of one or more program statements that can be called by name. It helps code organization and you can use block of code multiple times. But when method is called it runs one action. Method does not have {} curly braces for statements.
Here is an instance where Function and method are used:
function yourMessage() {
alert("Your message here");
} // created and declared your Function.
yourMessage(); //Function called and runs all the statements.
Hope this helps as well!