@@ -983,14 +983,9 @@ exports.Class = class Class extends Base
983
983
# Ensure that all functions bound to the instance are proxied in the
984
984
# constructor.
985
985
addBoundFunctions : (o ) ->
986
- if @boundFuncs .length
987
- o .scope .assign ' _this' , ' this'
988
- for [name, func] in @boundFuncs
989
- lhs = new Value (new Literal " this" ), [new Access name]
990
- body = new Block [new Return new Literal " #{ @ctor .name } .prototype.#{ name .value } .apply(_this, arguments)" ]
991
- rhs = new Code func .params , body, ' boundfunc'
992
- bound = new Assign lhs, rhs
993
- @ctor .body .push bound
986
+ for bvar in @boundFuncs
987
+ lhs = (new Value (new Literal " this" ), [new Access bvar]).compile o
988
+ @ctor .body .unshift new Literal " #{ lhs} = #{ utility ' bind' } (#{ lhs} , this)"
994
989
return
995
990
996
991
# Merge the properties from a top-level object as prototypal properties
@@ -1020,7 +1015,7 @@ exports.Class = class Class extends Base
1020
1015
else
1021
1016
assign .variable = new Value (new Literal (name), [(new Access new Literal ' prototype' ), new Access base ])
1022
1017
if func instanceof Code and func .bound
1023
- @boundFuncs .push [ base, func]
1018
+ @boundFuncs .push base
1024
1019
func .bound = no
1025
1020
assign
1026
1021
compact exprs
@@ -2122,6 +2117,11 @@ UTILITIES =
2122
2117
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; }
2123
2118
"""
2124
2119
2120
+ # Create a function bound to the current value of "this".
2121
+ bind : -> '''
2122
+ function(fn, me){ return function(){ return fn.apply(me, arguments); }; }
2123
+ '''
2124
+
2125
2125
# Discover if an item is in an array.
2126
2126
indexOf : -> """
2127
2127
[].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