title: Extending Javascript objects with CoffeeScript
body: <p>I want to add the ability to extend javascript objects by adding a method to the prototype.<br/>
  The method will receive one or more other objects and will add all of the key/values to <code>this</code>.<br/>
  This is what I came up with:</p>
  
  <pre><code>Object::extend = (objects...) -&gt;
      @[key] = value for key, value of object for object in objects
  </code></pre>
  
  <p>or this:</p>
  
  <pre><code>Object::extend = (objects...) -&gt;
      for object in objects
          for key, value of object
              @[key] = value 
  </code></pre>
  
  <p>Both work as expected, and compile into the same javascript code:</p>
  
  <pre><code>var __slice = [].slice;
  
  Object.prototype.extend = function() {
    var key, object, objects, value, _i, _len, _results;
    objects = 1 &lt;= arguments.length ? __slice.call(arguments, 0) : [];
    _results = [];
    for (_i = 0, _len = objects.length; _i &lt; _len; _i++) {
      object = objects[_i];
      _results.push((function() {
        var _results1;
        _results1 = [];
        for (key in object) {
          value = object[key];
          _results1.push(this[key] = value);
        }
        return _results1;
      }).call(this));
    }
    return _results;
  };
  </code></pre>
  
  <p>What I'm not too happy about is the whole results thing that is created per for loop which is completely redundant for my purpose.<br/>
  Is there a way to get a code more like:</p>
  
  <pre><code>Object.prototype.extend = function() {
    var key, object, objects, value, _i, _len;
    objects = 1 &lt;= arguments.length ? __slice.call(arguments, 0) : [];
    for (_i = 0, _len = objects.length; _i &lt; _len; _i++) {
      object = objects[_i];
      (function() {
        for (key in object) {
          value = object[key];
          this[key] = value;
        }
      }).call(this);
    }
  };
  </code></pre>
  
  <p>Thanks.</p>
  
  <hr>
  
  <h1>Edit</h1>
  
  <p>I'm aware that I can simply <a href="http://coffeescript.org/#embedded" rel="nofollow">embed</a> javascript code, but looking for a coffeescript solution.</p>