@@ -868,9 +868,22 @@ exports.Class = class Class extends Base
868
868
# Ensure that all functions bound to the instance are proxied in the
869
869
# constructor.
870
870
addBoundFunctions : (o ) ->
871
- for bvar in @boundFuncs
872
- lhs = (new Value (new Literal " this" ), [new Access bvar]).compile o
873
- @ctor .body .unshift new Literal " #{ lhs} = #{ utility ' bind' } (#{ lhs} , this)"
871
+ if @boundFuncs .length
872
+ o .scope .assign ' _this' , ' this'
873
+ for [name, func] in @boundFuncs
874
+ lhs = new Value (new Literal " this" ), [new Access name]
875
+ body = new Block [new Return new Literal " #{ @ctor .name } .prototype.#{ name .value } .apply(_this, arguments)" ]
876
+ rhs = new Code func .params , body, ' boundfunc'
877
+ bound = new Assign lhs, rhs
878
+
879
+ @ctor .body .unshift bound
880
+
881
+ # {base} = assign.variable
882
+ # lhs = (new Value (new Literal "this"), [new Access base]).compile o
883
+ # @ctor.body.unshift new Literal """#{lhs} = function() {
884
+ # #{o.indent} return #{@ctor.name}.prototype.#{base.value}.apply(_this, arguments);
885
+ # #{o.indent}}\n
886
+ # """
874
887
return
875
888
876
889
# Merge the properties from a top-level object as prototypal properties
@@ -900,7 +913,7 @@ exports.Class = class Class extends Base
900
913
else
901
914
assign .variable = new Value (new Literal (name), [(new Access new Literal ' prototype' ), new Access base ])
902
915
if func instanceof Code and func .bound
903
- @boundFuncs .push base
916
+ @boundFuncs .push [ base, func]
904
917
func .bound = no
905
918
assign
906
919
compact exprs
@@ -1960,11 +1973,6 @@ UTILITIES =
1960
1973
function(child, parent) { for (var key in parent) { if (#{ utility ' 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; }
1961
1974
"""
1962
1975
1963
- # Create a function bound to the current value of "this".
1964
- bind : -> '''
1965
- function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
1966
- '''
1967
-
1968
1976
# Discover if an item is in an array.
1969
1977
indexOf : -> """
1970
1978
[].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }
0 commit comments