enabling a variable number of arguments to be passed to constructor functions.

This commit is contained in:
Jeremy Ashkenas
2010-07-12 22:42:21 -04:00
parent d2c820b5d7
commit 534e2a25de
3 changed files with 32 additions and 2 deletions

View File

@@ -440,6 +440,8 @@ exports.CallNode: class CallNode extends BaseNode
# If you call a function with a splat, it's converted into a JavaScript
# `.apply()` call to allow an array of arguments to be passed.
# If it's a constructor, then things get real tricky. We have to inject an
# inner constructor in order to be able to pass the varargs.
compileSplat: (o) ->
meth: if @variable then @variable.compile(o) else @superReference(o)
obj: @variable and @variable.source or 'this'
@@ -447,7 +449,17 @@ exports.CallNode: class CallNode extends BaseNode
temp: o.scope.freeVariable()
obj: temp
meth: "($temp = ${ @variable.source })${ @variable.last }"
"${@prefix()}${meth}.apply($obj, ${ @compileSplatArguments(o) })"
if @isNew
utility 'extends'
"""
(function() {
${@idt(1)}var ctor = function(){ };
${@idt(1)}__extends(ctor, $meth);
${@idt(1)}return ${meth}.apply(new ctor, ${ @compileSplatArguments(o) });
$@tab}).call(this)
"""
else
"${@prefix()}${meth}.apply($obj, ${ @compileSplatArguments(o) })"
#### ExtendsNode