What is the difference between function and method?

Asked

Viewed 80 times

0

Several references mention syntax commands Javascript as addEventListener(), pop(), querySelector() and etc as methods:

trainingweb

w3schools

But the very MDN Web Docs defines these commands as functions and not methods:

MDN Web Docs

So I stopped here I don’t know what the differences between the two in the case for me so far a method is a function within an object:

let a = {
  b: function() {
    return "Olá";
  }
}

So if I wanted to, for example, check if an element has a certain class I would have to write something like classList.container('class1') that is to say classList is an object and I am accessing your method which is container().

Already a function for me is something that does not have the need to declare something earlier as, by exeplo:

function res(a, b) {
    return a * b;
}

That is my view, but on the question of right or wrong what is the difference between the two terms?

  • It also addresses the theme: https://answall.com/questions/212265/fun%C3%A7%C3%A3o-e-m%C3%A9todo-s%C3%A3o-a-mesma-coisa

  • Vlw! by the references although not having a concrete source of the specification beyond Wikipedia deducing the real meaning of each of the terms I will continue with the above thinking and that of the answers of the links cited.

1 answer

-2

Method is a function associated with the data structure of an object. When you call a.b(), it is assumed that b() will read and/or write data on object "a". This can happen indirectly if a.b() calls other methods of the object.

Can I write a method that doesn’t actually interact with the object at all? Can, but should not; it should be a static method (associated with a class, not an object) or even a function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.