-
Notifications
You must be signed in to change notification settings - Fork 2k
1.6.0 Broke overwriting a bound method #2773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
Well, in 1.5 this was done in the constructor when you bind methods: function Foo() {
this.someMethod = __bind(this.someMethod, this);
} While in 1.6 this is how it looks: function Foo() {
_this = this;
this.someMethod = function() {
return Foo.prototype.someMethod.apply(_this, arguments);
};
} |
return Bar.__super__.constructor.apply(this, arguments); |
@michaelficarra, i think @ansman is right. The change of the added Compare the 1.4.0 output: // Generated by CoffeeScript 1.4.0
(function() {
var Bar, Foo,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Foo = (function() {
function Foo() {
this.someMethod = __bind(this.someMethod, this);
}
Foo.prototype.someMethod = function() {
return alert(Foo);
};
return Foo;
})();
Bar = (function(_super) {
__extends(Bar, _super);
function Bar() {
this.someMethod = __bind(this.someMethod, this);
return Bar.__super__.constructor.apply(this, arguments);
}
Bar.prototype.someMethod = function() {
return alert(Bar);
};
return Bar;
})(Foo);
new Bar().someMethod();
}).call(this); With the 1.6.0 one: // Generated by CoffeeScript 1.6.0
(function() {
var Bar, Foo,
_this = this,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Foo = (function() {
function Foo() {
var _this = this;
this.someMethod = function() {
return Foo.prototype.someMethod.apply(_this, arguments);
};
}
Foo.prototype.someMethod = function() {
return alert(Foo);
};
return Foo;
})();
Bar = (function(_super) {
__extends(Bar, _super);
function Bar() {
var _this = this;
this.someMethod = function() {
return Bar.prototype.someMethod.apply(_this, arguments);
};
return Bar.__super__.constructor.apply(this, arguments);
}
Bar.prototype.someMethod = function() {
return alert(Bar);
};
return Bar;
})(Foo);
new Bar().someMethod();
}).call(this); The difference is that CS used to pass Now that Shouldn't be a difficult fix i think. |
Pushed a fix that should do the trick. But there may still be subtleties. Take a peek and let me know what you think. |
@epidemian: Ah, yes, my mistake. I was confused. So in which way do we want to fix it? We could either apply the superclass constructor before any bound method overrides or add some condition around the assignment of the bound method. edit: Looks like Jeremy was too fast. Re-ordering it is. |
I'm still concerned about cases where either the subclass constructor, or the superclass constructor want to use one of these bound functions themselves. |
edit: Never mind! I need to go to bed. |
I'm pretty sure that won't work. If |
Whoops! |
Couldn't it be fixed by using the methods from function A() {
var _this = this, _proto = _this.constructor.prototype;
this.fat = function() {
return _proto.fat.apply(_this, arguments);
};
} |
@shesek: That's a good idea. CoffeeScriptRedux compiles in this way: Bar = function (super$) {
extends$(Bar, super$);
function Bar() {
var instance$;
instance$ = this;
this.someMethod = function () {
return Bar.prototype.someMethod.apply(instance$, arguments);
};
}
Bar.prototype.someMethod = function () {
return alert('Bar');
};
return Bar;
}(Foo); I'm thinking your way is better, since you have a reference to the actual |
@michaelficarra I was using
edit: actually, since this is only relevant in classes defined using coffee's |
Consider this code:
In 1.6.0 it alerts
Foo
while in 1.5.0 it alertsBar
.I think that this is not the intended behaviour, how does one overwrite a method now?
It seems to be caused by the super constructor running last in the child's constructor, shouldn't it be the other way around?
The text was updated successfully, but these errors were encountered: