title: This JavaScript syntax I haven't seen till now, what does it do really?
body: <p>Today I saw a JavaScript syntax (when invoking a function) that is unfamiliar to me. It was like:</p>
  
  <pre><code>def('Person') ({
    init: function(name) {this.name=name;}
    ,speak: function(text) {alert(text || 'Hi, my name is ' + this.name);}
  });
  </code></pre>
  
  <p>, and</p>
  
  <pre><code>def('Ninja') &lt;&lt; Person ({
    kick: function() {this.speak('I kick u!');}
  });
  </code></pre>
  
  <p>1: What happens with the object within the parentheses in the first example? It is handled by the <code>def</code> function somehow, but I don't understand what is going on here (see the <code>def</code> function below). Where does the object go?</p>
  
  <p>2: About the same thing again, but a use of the <code>&lt;&lt;</code> operator that I never seen (I think!). What's that all about?</p>
  
  <p>The code is from <a href="http://gist.github.com/474994" rel="nofollow noreferrer">http://gist.github.com/474994</a>, where Joe Dalton has made a small JavaScript-OO-inheritance thing (it is apparently a fork of someone else's work, but quite thoroughly rewritten, as it seems). Maybe you want to check it out there for the stuff referenced by the <code>def</code> function, which I give you here:</p>
  
  <pre><code>function def(klassName, context) {
    context || (context = global);
  
    // Create class on given context (defaults to global object)
    var Klass =
      context[klassName] = function Klass() {
  
        // Called as a constructor
        if (this != context) {
  
          // Allow the init method to return a different class/object
          return this.init &amp;&amp; this.init.apply(this, arguments);
        }
  
        // Called as a method
        // defer setup of superclass and plugins
        deferred._super = Klass;
        deferred._plugins = arguments[0] || { };
      };
  
    // Add static helper method
    Klass.addPlugins = addPlugins;
  
    // Called as function when not
    // inheriting from a superclass
    deferred = function(plugins) {
      return Klass.addPlugins(plugins);
    };
  
    // valueOf is called to set up
    // inheritance from a superclass
    deferred.valueOf = function() {
      var Superclass = deferred._super;
      if (!Superclass)
          return Klass;
      Subclass.prototype = Superclass.prototype;
      Klass.prototype = new Subclass;
      Klass.superclass = Superclass;
      Klass.prototype.constructor = Klass;
      return Klass.addPlugins(deferred._plugins);
    };
    return deferred;
  }
  </code></pre>