title: Defining JavaScript functions inside vs. outside of a class
body: <p>I'm trying to figure out if there's any different when defining functions inside or outside of a class in JavaScript. <strong>Why would I choose to do it one way over the other?</strong> (Notice my getName [inside class] and getName2 [outside of class]).</p>
  
  <pre><code>class TestClass {
      constructor(myName) {
          this.name = myName;
      }
  
      getName() {
          return this.name;
      }
  }
  
  TestClass.getName2 = function() {
      //won't actually print the name variable set since not associated with an instance of the class?
      console.log(this.name);
  };
  
  var test = new TestClass("Joe");
  
  console.log(test.getName());
  
  ///////////////
  
  TestClass.getName2();
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Joe
  TestClass
  </code></pre>
  
  <p>The only difference I can really see so far through my testing here is that I cannot access <code>this.name</code> within my getName2 since I believe it's not associated with any instance of the TestClass. So my getName2 is almost like a static class function where it's not associated with an instance of the class?? Please help me clarify this and why I would choose to implement a function one way over the other.</p>