title: How to call function from the constructor of a JS class
body: <p>I'm creating a matrix class (to be used in a react app) which I'd like define this way:</p>
  
  <pre><code>class Matrix extends Object {
      constructor(r, c) {
          super();
          this.rows = new Array(r);
          for (var i=0; i&lt;r; i++) {
              this.rows[i] = new Array(c);
          }
          this.assign((v,i,j) =&gt; (i===j)? 1:0);   // ERROR HERE
      }
      assign(f) {
          for (var i=0; i&lt;this.rows.length; i++) {
              for (var j=0; j&lt;this.rows[i].length; j++) {
                  this.rows[i][j] = f(this.rows[i][j], i, j);
              }
          }
      }
      // and so on
  </code></pre>
  
  <p>This compiles okay via webpack, but while running, I get an error in the chrome console saying that <strong>_this.assign is not a function</strong>.</p>
  
  <p>I can make it work this way:</p>
  
  <pre><code>constructor(r, c) {
      super();
      this.rows = new Array(r);
      for (var i=0; i&lt;r; i++) {
          this.rows[i] = new Array(c);
      }
      this.assign = function(f) {
          for (var i=0; i&lt;r; i++) {
              for (var j=0; j&lt;r; j++) {
                  this.rows[i][j] = f(this.rows[i][j], i, j);
              }
          }
      };
      this.assign((v,i,j) =&gt; (i===j)? 1:0);
  }
  </code></pre>
  
  <p>But that's wrong, right?  I shouldn't have to define all of an object's functions in the constructor, right?  In the very same file, I define react classes like:</p>
  
  <pre><code>class MatrixComponent extends React.Component { ... }
  </code></pre>
  
  <p>and those are able to call functions without defining them in the constructor.  What am I missing?</p>