title: JavaScript extending types return
body: <p>I'm actually studying  Crockford's <em>Javascript: the good parts</em>. I am new to JavaScript so I'm having a difficult time to understand how this code works:</p>
  
  <pre><code>Function.prototype.method = function (name, func) {
      this.prototype[name] = func;
      return this;
  };
  </code></pre>
  
  <p><strong>Here's what I think:</strong></p>
  
  <p>Being a method (a function inside an object), <code>this</code> points to the <code>Function</code> object, but why the need to return the object since i'm having access on it from inside the method? If i am right, <code>this</code> is a reference, not a local copy, so:</p>
  
  <pre><code>Function.prototype.method = function (name, func) {
      this.prototype[name] = func;
  };
  </code></pre>
  
  <p>Should work as well.</p>
  
  <p>From the other side, in JavaScript a function without a return statement returns <code>undefined</code> and assign it to <code>Function.prototype.method</code>. </p>
  
  <h1>The Question</h1>
  
  <p><strong>What's the point of returning <code>this</code>?</strong></p>
  
  <hr>
  
  <h2>Working example #1</h2>
  
  <pre><code>Function.prototype.method = function (name, func) {
      this.prototype[name] = func;
      return this;
  };
  var add = function(a, b) {
      return a+b;
  };
  
  Function.method('add', add);
  var f = function() {};
  
  print(f.add(1,2));
  
  Number.method('integer', function () {
          return Math[this &lt; 0 ? 'ceil' : 'floor'](this);
          });
  
  print((-10/3).integer());
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <blockquote>
    <p>-3 
    3</p>
  </blockquote>
  
  <hr>
  
  <h2>Working Example #2</h2>
  
  <pre><code>Function.prototype.method = function (name, func) {
      this.prototype[name] = func;
  };
  
  var add = function(a, b) {
      return a+b;
  };
  
  Function.method('add', add);
  var f = function() {};
  
  print(f.add(1,2));
  
  Number.method('integer', function () {
          return Math[this &lt; 0 ? 'ceil' : 'floor'](this);
          });
  
  print((-10/3).integer());
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <blockquote>
    <p>-3
     3</p>
  </blockquote>