michaelficarra/CoffeeScriptRedux

assign to `this.param` in prototype method signature

DavidSouther opened this issue · 2 comments

In coffeescript,

class Foo
    constructor: ->
        @args = []

    act: (@args...)->

compiles to

var Foo,
  __slice = [].slice;

Foo = (function() {
  function Foo() {
    this.args = [];
  }

  Foo.prototype.act = function() {
    var args;
    args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
    this.args = args;
  };

  return Foo;

})();

http://coffeescript.org/#try:class%20Foo%0A%20%20%20%20constructor%3A%20-%3E%0A%20%20%20%20%20%20%20%20%40args%20%3D%20%5B%5D%0A%0A%20%20%20%20act%3A%20(%40args...)-%3E%0A

Which has the effect of setting this.args to the passed args array, or using the old value if nothing past.

In Redux, it compiles to

void function () {
  Foo = function () {
    function Foo() {
      this.args = [];
    }
    Foo.prototype.act = function (this.args) {
      this.args = 1 <= arguments.length ? [].slice.call(arguments, 0) : [];
    };
    return Foo;
  }();
}.call(this);

http://michaelficarra.github.io/CoffeeScriptRedux/#try:class%20Foo%0A%20%20%20%20constructor%3A%20-%3E%0A%20%20%20%20%20%20%20%20%40args%20%3D%20%5B%5D%0A%0A%20%20%20%20act%3A%20(%40args...)-%3E%0A

Notice that the method signature is now this.args - which is a syntax error in JS.

This is fixed on master.

// Generated by CoffeeScript 2.0.0-beta9-dev
void function () {
  Foo = function () {
    function Foo() {
      this.args = [];
    }
    Foo.prototype.act = function () {
      this.args = arguments.length > 0 ? [].slice.call(arguments, 0) : [];
    };
    return Foo;
  }();
}.call(this);

Oh, awesome! When will beta9 get released to NPM?