title: How do I inherit javascript functions ?
body: <pre><code>// Don't break the function prototype.
  // pd - https://github.com/Raynos/pd
  var proto = Object.create(Function.prototype, pd({
    "prop": 42
  }));
  
  var f = function() { return "is a function"; };
  f.__proto__ = proto;
  
  console.log(f.hasOwnProperty("prop")); // false
  console.log(f.prop); // 42
  console.log(f()); // "is a function"
  </code></pre>
  
  <p><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/Proto" rel="noreferrer"><code>.__proto__</code></a> is non-standard and deprecated.</p>
  
  <p>How am I supposed to inherit prototypically creating an object but having that object be a function.</p>
  
  <p><code>Object.create</code> returns an Object not a Function.</p>
  
  <p><code>new Constructor</code> returns an Object not a Function.</p>
  
  <p><strong>Motivation:</strong> - A cross-browser <code>finherit</code></p>
  
  <pre><code>var finherit = function (parent, child) {
      var f = function() { 
          parent.apply(this, arguments);
          child.apply(this, arguments);
      };
      f.__proto__ = parent;
      Object.keys(child).forEach(function _copy(key) {
          f[key] = child[key];
      });
      return f;
  };
  </code></pre>
  
  <p>I don't believe this is possible, so we should probably propose a <code>Function.create</code> to the es-discuss mailing list</p>
  
  <pre><code>/*
    Creates a new function whose prototype is proto.
    The function body is the same as the function fbody.
    The hash of propertydescriptors props is passed to defineproperties just like
    Object.create does.
  */
  Function.create = (function() {
    var functionBody = function _getFunctionBody(f) {
      return f.toString().replace(/.+\{/, "").replace(/\}$/, "");
    };
    var letters = "abcdefghijklmnopqrstuvwxyz".split("");
  
    return function _create(proto, fbody, props) {
      var parameters = letters.slice(0, fbody.length);
      parameters.push(functionBody(fbody));
      var f = Function.apply(this, parameters);
      f.__proto__ = proto;
      Object.defineProperties(f, props);
      return f;
    };
  })();
  </code></pre>
  
  <p><a href="https://mail.mozilla.org/pipermail/es-discuss/2011-September/016743.html" rel="noreferrer">Related es-discuss mail</a></p>
  
  <p>As mentioned in the es-discuss thread there exists a ES:strawman <a href="http://wiki.ecmascript.org/doku.php?id=harmony:proto_operator" rel="noreferrer"><code>&lt;|</code></a> prototype operator which would allow for this.</p>
  
  <p>Let's see what it would look like using <code>&lt;|</code></p>
  
  <pre><code>var f1 = function () {
    console.log("do things");
  };
  
  f1.method = function() { return 42; };
  
  var f2 = f1 &lt;| function () {
    super();
    console.log("do more things");
  }
  console.log(f1.isPrototypeOf(f2)); // true
  console.log(f2()); // do things do more things
  console.log(f2.hasOwnProperty("method")); // false
  console.log(f2.method()); // 42
  </code></pre>