function vs method

Llebombjames19/15/2021
Anyone know of a good guide that explains the difference between a function and a method? I don't really understand when one should be used over another since, if I'm not mistaken, they're both functions at a base level.
Ccalego9/15/2021
I don't know of any guides about this (also am interested if someone finds one), but I can answer your question by saying the words "Function" and "method" are used pretty interchangably in the javascript ecocsytem.

You could argue "method" refers to a "function which belongs to something (e.g. a class or other object)".

But in the end all methods are functions, though with the above argument not all functions are methods.
Llebombjames19/15/2021
You could argue "method" refers to a "function which belongs to something (e.g. a class or other object)".
This was my understanding, although I didn't think it could be that simple (nothing in programming is!)

The main thing that kinda threw me off was args in relations to methods, I may have just not searched enough but can you have a method with args? Something like thing.method(arg1, arg2)?
Ccalego9/15/2021
Yep
Ccalego9/15/2021
So in your case thing might look like this:
const thing = {
  method: function (arg1, arg2) { ... }
}
Ccalego9/15/2021
which could just as easily be written like this:
function someArbitraryName (arg1, arg2) { ... }

const thing = {
  method: someArbitraryName
}
Ccalego9/15/2021
For like 99.99999% of use cases those are identical.

Where 'method' really comes into play is with classes and this.
Ccalego9/15/2021
// capitalize first letter of a class is convention
class Thing {
  method(arg1, arg2) {
    this.otherMethod();
  }

  otherMethod() { ... }
}

is different from the following:
function someArtitraryName (arg1, arg2) {
  this.otherMethod();
}

class Thing {
  method = someArbitraryName // no idea if this works tbh
  
  otherMethod() {...}
}

(i'm like 60% sure about that, this is confusing af and I work professionally in JS-land)
Ccalego9/15/2021
A general rule of thumb I follow is: "If this ever matters, you should restructure your code so that it no longer matters" 😅
Llebombjames19/15/2021
This sounds like great advice! So this in the first example just points to the object/class the method is in? So is this.otherMethod() effectively method.otherMethod() or Thing.otherMethod()?
Ccalego9/15/2021
I get better every time I explain this give me a sec
Ccalego9/15/2021
this in the first example is 'effectively'* Thing.otherMethod()

* big caveat.

this is "this instance of Thing", where the syntax Thing.otherMethod would call a static method named otherMethod on that class.

class Thing {
  // non-static properties
  CONST = 'some string';

  foo() {
    console.log('non-static', this.CONST);
  }
  bar() {
    this.foo(); // should log "non-static some string"
  }

  // static properties
  static CONST = 'some other string';

  static foo() {
    console.log('static', this.CONST);
  }

  static bar() {
    this.foo(); // should log "static some other string"
  }
}

// ...

Thing.bar(); // calls the `static bar`

new Thing().bar(); // calls the `non-static bar`


A method could be defined as a function which is a 'property' of another .... thing. In this case a class.
Ccalego9/15/2021
confidence hovering around 80% for that example
Ccalego9/15/2021
I've never tried having the same named things in a class but some static and some not
Llebombjames19/15/2021
OK, I've wrapped my head around that... just about. The only thing I don't get is why Thing.bar() and new Thing().bar() call different things. My understanding is that () is used on classes for constructors?
Llebombjames19/15/2021
I think I'm a bit out of my depth here 😅
Ccalego9/15/2021
gonna be honest that's the part I'm not super confident about. it might be that both call the static bar
Ccalego9/15/2021
new Thing() instantiates the class. Way to think about that is "Thing" may be a generic template, but when you instantiate it you get one unique thing.

We're pretty far off course from your original question and firmly in the weeds of "Object oriented javascript".
Ccalego9/15/2021
I want to stress though that you can get by perfectly fine without a deep understanding of all the nuance here.
Ccalego9/15/2021
(I certainly do lol)
Llebombjames19/15/2021
I imagine you can, but I'm very much someone who learns how something works by knowing all the moving parts, and then forming my own mental "map" of how it all works. I'll take a look at that guide though, seems useful. Thanks for your help!
LTLLLeo The League Lion9/15/2021
@LebombJames gave :vote: LeaguePoints™ to @Calego (#1 • 1175)
UUUnknown User9/18/2021
78 Messages Not Public
Sign In & Join Server To View